UNPKG

@bracketed/logger

Version:

An alternative to your run-of-the-mill node console logging functions!

1 lines 3.41 kB
{"version":3,"sources":["../../../src/Utilities/Timestamp.ts"],"names":["LoggerTimestamp","utc","color","timestamp","options","pattern","formatter","run","Date","displayUTC","date","display","result"],"mappings":"wIAQO,WAPP,CAAA,OAOaA,CAAAA,CAAAA,IAAAA,CAAAA,6BAWLC,CAAAA,GAMAC,CAAAA,eAQP,CAAA,WAAqD,CAAC,CAAA,CAAG,EACxD,CAAA,CAAA,IAAKC,CAAAA,cAA0BC,SAAQC,UAAW,EAAA,2BAC7CJ,CAAAA,GAAcA,MAAO,EAC1B,KAAA,CAAA,UAAqBC,CAAAA,CAAAA,CAAAA,KAAU,OAAO,CAAA,IAAO,CAAA,IAAgBE,WAAQF,QACrE,CAAA,CAAA,cAAyBI,CAAAA,CAAAA,CAAAA,SAAeH,GAAc,CAAA,EAAGA,CAAAA,OAOnDI,CAAAA,EAAAA,CAAAA,GACN,EAAA,CAAA,UAAiBC,IACF,OAAKP,CAAAA,GAAM,CAAA,IAAKE,CAAAA,SAAUM,CAAAA,UAAWC,CAAQ,CAAA,CAAA,CAAA,cAAeC,CAAAA,QAC3E,CAAA,CAAA,CAAA,OAAO,cAAe,CAAA,IAAKT,CAAAA,UAAaA,CAAAA,KAAMK,CAAAA,GAAIK,CAAUA,CAAAA,CAC7D,CACD,CAAA,CAAA,CAAA","file":"Timestamp.mjs","sourcesContent":["import type { LoggerStyleResolvable } from '../Style/Resolvable';\nimport { LoggerStyle } from '../Style/Style';\nimport { Timestamp } from '../Timestamper/Timestamp';\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"]}