@bracketed/logger
Version:
An alternative to your run-of-the-mill node console logging functions!
1 lines • 7.06 kB
Source Map (JSON)
{"version":3,"sources":["../../../src/Logger/Logger.ts"],"names":["l","BuiltinLogger","console","formats","depth","prefix","level","Info","join","options","undefined","Console","stdout","process","stderr","createFormatMap","format","defaultFormat","write","values","levels","LogLevel","None","formatter","run","preprocess","inspectOptions","colors","map","value","Reflect","defaults","none","Trace","ensureDefaultLevel","Debug","Warn","Error","Fatal","fatal","f","g","color","name","LoggerLevel","h","timestamp","infix","padEnd"],"mappings":"0UAcO,IAAA,CAAA,CAAA,cAdPA,MAAA,CAAA,OAc4BC,GAAAA,CAAAA,IAAAA,CAAAA,QAKXC,EAAAA,CAAAA,OAMAC,CAAAA,QAYAC,IAAAA,CAAAA,KAMCC,CAAAA,MAEjB,CAAA,WAA6C,CAAG,CAC/C,UAAcC,CAAAA,CAAAA,CAAAA,KAAkBC,EAAAA,QAAI,CAAA,IAEpC,CAAA,CAAKC,IAAAA,CAAOC,IAAQD,CAAAA,CAAAA,CAAAA,IAAQ,MACvBJ,IAAAA,CAAAA,aACL,EAAA,CAAA,CAAKC,IAAAA,CAAAA,eAA2BK,EAAAA,MAEhC,CAAKR,YAAcS,CAAAA,IAAgBC,OAAAA,CAAAA,CAAAA,CAAAA,MAAUC,EAAAA,OAAQD,CAAAA,eAA0BC,EAAAA,OAAQC,CAAAA,MACvF,CAAA,CAAKX,YAAU,CAAKY,IAAAA,CAAAA,eAAwBC,CAAAA,CAAAA,CAAAA,MAAgBC,CAAAA,CAAAA,CAAAA,aAQ7CC,EAAAA,CAAAA,KAAMZ,CAAoBa,EAAkC,GAC3E,CAAIb,CAAAA,CAAQ,GAAA,CAAA,CAAKA,UAAO,CAAA,WAET,CAAA,CAAKc,IAAAA,CAAAA,MAAWd,CAAAA,GAAU,CAAA,CAAA,CAAA,EAAA,QAClBH,IAAAA,CAAAA,OAAYG,CAAAA,GAAU,CAAA,CAAA,CAAA,EAAKH,YAAYkB,CAAAA,GAAS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IAAAA,CAAAA,kBAA2BF,CAAAA,CAAAA,CAAMD,IAAiB,CAAA,CAAA,CAAAU,KAAA,CACvE,EAGQP,uBAGTQ,EACAC,CAAAA,CACC,CACD,YAAoB,CAAIC,CAAAA,IAEjBC,WAAA,CAAA,CAAA,CAAID,CAAAA,IACVC,WAAGd,CAAAA,CACHe,cAAoBA,CAAAA,CAAAA,CAAAA,SAAc,GAAO,IAAA,CAAO,IAAMf,CAAAA,CAASe,cAAkBJ,UACjFK,CAAAA,CAAAA,CAAAA,CAAAA,cACML,CAAMC,GAAKK,CAAAA,CAAAA,CAAAA,CAAAA,OAAU,CAAA,CAAA,GAAA,CAAA,CAAA,CAAW,GAAA,EAAK3C,WAAY,CAAA,CAAA,EAAKA,WAAM,CAAQ,KACvE,EAAA,CAAG,CAAA,CAAA,CAAA,EAAKA,IAAAA,CAAAA,UAAiBA,IAAAA,CAAAA,MAAM,CAAQ,GAAA,CAC3C,CAAA,EAEF,CAAA,CAAA,CAAA,CAAA,CAAA","file":"Logger.mjs","sourcesContent":["import { bgRed, cyan, gray, isColorSupported, magenta, red, white, yellow, type Color } from '../Colouring';\n\nimport { Console } from 'node:console';\nimport { inspect, type InspectOptions } from 'node:util';\n\nimport { LogLevel } from './ILogger/ILogLevel';\nimport type { LogMethods } from './ILogger/ILogMethods';\nimport { Logger as BuiltinLogger } from './ILogger/ILoggerStyle';\n\nimport type { LoggerFormatOptions } from './FormatOptions';\nimport { LoggerLevel } from './Level';\nimport type { LoggerLevelOptions } from './LevelOptions';\nimport type { LoggerOptions } from './Options';\n\nexport class Logger extends BuiltinLogger {\n\t/**\n\t * The console this writes to.\n\t * @since 1.0.0\n\t */\n\tpublic readonly console: Console;\n\n\t/**\n\t * The formats supported by the logger.\n\t * @since 1.0.0\n\t */\n\tpublic readonly formats: Map<LogLevel, LoggerLevel>;\n\n\t/**\n\t * The string `write` will join values by.\n\t * @since 1.0.0\n\t */\n\tpublic readonly join: string;\n\n\t/**\n\t * The inspect depth when logging objects.\n\t * @since 1.0.0\n\t */\n\tpublic readonly depth: number;\n\n\t/**\n\t * A prefix for messages logged by the logger.\n\t * @since 1.0.11\n\t */\n\tprivate readonly prefix: string | undefined;\n\n\tpublic constructor(options: LoggerOptions = {}) {\n\t\tsuper(options.level ?? LogLevel.Info);\n\n\t\tthis.join = options.join ?? ' ';\n\t\tthis.depth = options.depth ?? 2;\n\t\tthis.prefix = options.prefix ?? undefined;\n\n\t\tthis.console = new Console(options.stdout ?? process.stdout, options.stderr ?? process.stderr);\n\t\tthis.formats = this.createFormatMap(options.format, options.defaultFormat);\n\t}\n\n\t/**\n\t * Writes the log message given a level and the value(s).\n\t * @param level The log level.\n\t * @param values The values to log.\n\t */\n\tpublic override write(level: LogLevel, ...values: readonly unknown[]): void {\n\t\tif (level < this.level) return;\n\n\t\tconst method = this.levels.get(level) ?? 'log';\n\t\tconst formatter = this.formats.get(level) ?? this.formats.get(LogLevel.None)!;\n\n\t\tthis.console[method](formatter.run(this.preprocess(values)));\n\t}\n\n\t/**\n\t * Pre-processes an array of values.\n\t * @since 1.0.0\n\t * @param values The values to pre-process.\n\t */\n\tprotected preprocess(values: readonly unknown[]) {\n\t\tconst inspectOptions: InspectOptions = { colors: isColorSupported, depth: this.depth };\n\t\treturn values\n\t\t\t.map((value) => (typeof value === 'string' ? value : inspect(value, inspectOptions)))\n\t\t\t.join(this.join);\n\t}\n\n\tprivate get levels() {\n\t\treturn Reflect.get(BuiltinLogger, 'levels') as Map<LogLevel, LogMethods>;\n\t}\n\n\t/**\n\t * Gets whether or not colorette is enabled.\n\t * @since 1.0.0\n\t */\n\tpublic static get stylize() {\n\t\treturn isColorSupported;\n\t}\n\n\tprotected createFormatMap(options: LoggerFormatOptions = {}, defaults: LoggerLevelOptions = options.none ?? {}) {\n\t\treturn new Map<LogLevel, LoggerLevel>([\n\t\t\t[LogLevel.Trace, this.ensureDefaultLevel(options.trace, defaults, gray, 'TRACE')],\n\t\t\t[LogLevel.Debug, this.ensureDefaultLevel(options.debug, defaults, magenta, 'DEBUG')],\n\t\t\t[LogLevel.Info, this.ensureDefaultLevel(options.info, defaults, cyan, 'INFO')],\n\t\t\t[LogLevel.Warn, this.ensureDefaultLevel(options.warn, defaults, yellow, 'WARN')],\n\t\t\t[LogLevel.Error, this.ensureDefaultLevel(options.error, defaults, red, 'ERROR')],\n\t\t\t[LogLevel.Fatal, this.ensureDefaultLevel(options.fatal, defaults, bgRed, 'FATAL')],\n\t\t\t[LogLevel.None, this.ensureDefaultLevel(options.none, defaults, white, '')],\n\t\t]);\n\t}\n\n\tprotected ensureDefaultLevel(\n\t\toptions: LoggerLevelOptions | undefined,\n\t\tdefaults: LoggerLevelOptions,\n\t\tcolor: Color,\n\t\tname: string\n\t) {\n\t\tif (options) return new LoggerLevel(options);\n\n\t\treturn new LoggerLevel({\n\t\t\t...defaults,\n\t\t\ttimestamp: defaults.timestamp === null ? null : { ...(defaults.timestamp ?? {}), color },\n\t\t\tinfix: name.length\n\t\t\t\t? `${color(name.padEnd(5, ' '))} - ${this.prefix ? `${this.prefix} - ` : ''}`\n\t\t\t\t: `${this.prefix ? `${this.prefix} - ` : ''}`,\n\t\t});\n\t}\n}\n"]}