@sapphire/plugin-logger
Version:
Plugin for @sapphire/framework to have pretty console output
1 lines • 3.29 kB
Source Map (JSON)
{"version":3,"sources":["../../../src/lib/LoggerTimestamp.ts"],"names":[],"mappings":";;;;;;;;;AAAA,SAAS,iBAAiB;AAC1B,SAAS,mBAA+C;AAMjD,IAAM,mBAAN,MAAM,iBAAgB;AAAA,EAyBrB,YAAY,UAAkC,CAAC,GAAG;AApBzD;AAAA;AAAA;AAAA;AAAA,wBAAO;AAMP;AAAA;AAAA;AAAA;AAAA,wBAAO;AAMP;AAAA;AAAA;AAAA;AAAA,wBAAO;AAMP;AAAA;AAAA;AAAA;AAAA,wBAAO;AAGN,SAAK,YAAY,IAAI,UAAU,QAAQ,WAAW,qBAAqB;AACvE,SAAK,MAAM,QAAQ,OAAO;AAC1B,SAAK,QAAQ,QAAQ,UAAU,OAAO,OAAO,IAAI,YAAY,QAAQ,KAAK;AAC1E,SAAK,YAAY,QAAQ,cAAc,CAAC,cAAc,GAAG,SAAS;AAAA,EACnE;AAAA;AAAA;AAAA;AAAA;AAAA,EAMO,MAAM;AACZ,UAAM,OAAO,oBAAI,KAAK;AACtB,UAAM,SAAS,KAAK,MAAM,KAAK,UAAU,WAAW,IAAI,IAAI,KAAK,UAAU,QAAQ,IAAI;AACvF,WAAO,KAAK,UAAU,KAAK,QAAQ,KAAK,MAAM,IAAI,MAAM,IAAI,MAAM;AAAA,EACnE;AACD;AAzC6B;AAAtB,IAAM,kBAAN","sourcesContent":["import { Timestamp } from '@sapphire/timestamp';\nimport { LoggerStyle, type LoggerStyleResolvable } from './LoggerStyle';\n\n/**\n * Logger utility that formats a timestamp.\n * @since 1.0.0\n */\nexport class LoggerTimestamp {\n\t/**\n\t * The timestamp used to format the current date.\n\t * @since 1.0.0\n\t */\n\tpublic timestamp: Timestamp;\n\n\t/**\n\t * Whether or not the logger will show a timestamp in UTC.\n\t * @since 1.0.0\n\t */\n\tpublic utc: boolean;\n\n\t/**\n\t * The logger style to apply the color to the timestamp.\n\t * @since 1.0.0\n\t */\n\tpublic color: LoggerStyle | null;\n\n\t/**\n\t * The final formatter.\n\t * @since 1.0.0\n\t */\n\tpublic formatter: LoggerTimestampFormatter;\n\n\tpublic constructor(options: LoggerTimestampOptions = {}) {\n\t\tthis.timestamp = new Timestamp(options.pattern ?? 'YYYY-MM-DD HH:mm:ss');\n\t\tthis.utc = options.utc ?? false;\n\t\tthis.color = options.color === null ? null : new LoggerStyle(options.color);\n\t\tthis.formatter = options.formatter ?? ((timestamp) => `${timestamp} - `);\n\t}\n\n\t/**\n\t * Formats the current time.\n\t * @since 1.0.0\n\t */\n\tpublic run() {\n\t\tconst date = new Date();\n\t\tconst result = this.utc ? this.timestamp.displayUTC(date) : this.timestamp.display(date);\n\t\treturn this.formatter(this.color ? this.color.run(result) : result);\n\t}\n}\n\n/**\n * The options for {@link LoggerTimestamp}.\n * @since 1.0.0\n */\nexport interface LoggerTimestampOptions {\n\t/**\n\t * The {@link Timestamp} pattern.\n\t * @since 1.0.0\n\t * @default 'YYYY-MM-DD HH:mm:ss'\n\t * @example\n\t * ```typescript\n\t * 'YYYY-MM-DD HH:mm:ss'\n\t * // 2020-12-23 22:01:10\n\t * ```\n\t */\n\tpattern?: string;\n\n\t/**\n\t * Whether or not the date should be UTC.\n\t * @since 1.0.0\n\t * @default false\n\t */\n\tutc?: boolean;\n\n\t/**\n\t * The color to use.\n\t * @since 1.0.0\n\t * @default colorette.reset\n\t */\n\tcolor?: LoggerStyleResolvable | null;\n\n\t/**\n\t * The formatter. See {@link LoggerTimestampFormatter} for more information.\n\t * @since 1.0.0\n\t * @default (value) => `${value} - `\n\t */\n\tformatter?: LoggerTimestampFormatter;\n}\n\n/**\n * The formatter used for {@link LoggerTimestampOptions}. This will be run **after** applying the color to the formatter.\n * @since 1.0.0\n */\nexport interface LoggerTimestampFormatter {\n\t/**\n\t * @param timestamp The output of {@link LoggerStyle.run} on {@link Timestamp.display}/{@link Timestamp.displayUTC}.\n\t * @since 1.0.0\n\t */\n\t(timestamp: string): string;\n}\n"]}