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AAO,GAAG,GAAG,CAAC,CAAC,QAAQ;AAAA,IAC3D,CAAC;AAAA,EACF;AACD;AA1F0C;AAAnC,IAAM,SAAN","sourcesContent":["import { Logger as BuiltinLogger, LogLevel, type LogMethods } from '@sapphire/framework';\nimport { bgRed, cyan, gray, isColorSupported, magenta, red, white, yellow, type Color } from 'colorette';\nimport { Console } from 'console';\nimport { inspect, type InspectOptions } from 'util';\nimport { LoggerLevel, type LoggerLevelOptions } from './LoggerLevel';\n\n/**\n * The logger class.\n * @since 1.0.0\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\tpublic constructor(options: LoggerOptions = {}) {\n\t\tsuper(options.level ?? LogLevel.Info);\n\n\t\tthis.console = new Console(options.stdout ?? process.stdout, options.stderr ?? process.stderr);\n\t\tthis.formats = Logger.createFormatMap(options.format, options.defaultFormat);\n\t\tthis.join = options.join ?? ' ';\n\t\tthis.depth = options.depth ?? 0;\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.map((value) => (typeof value === 'string' ? value : inspect(value, inspectOptions))).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\tprivate static createFormatMap(options: LoggerFormatOptions = {}, defaults: LoggerLevelOptions = options.none ?? {}) {\n\t\treturn new Map<LogLevel, LoggerLevel>([\n\t\t\t[LogLevel.Trace, Logger.ensureDefaultLevel(options.trace, defaults, gray, 'TRACE')],\n\t\t\t[LogLevel.Debug, Logger.ensureDefaultLevel(options.debug, defaults, magenta, 'DEBUG')],\n\t\t\t[LogLevel.Info, Logger.ensureDefaultLevel(options.info, defaults, cyan, 'INFO')],\n\t\t\t[LogLevel.Warn, Logger.ensureDefaultLevel(options.warn, defaults, yellow, 'WARN')],\n\t\t\t[LogLevel.Error, Logger.ensureDefaultLevel(options.error, defaults, red, 'ERROR')],\n\t\t\t[LogLevel.Fatal, Logger.ensureDefaultLevel(options.fatal, defaults, bgRed, 'FATAL')],\n\t\t\t[LogLevel.None, Logger.ensureDefaultLevel(options.none, defaults, white, '')]\n\t\t]);\n\t}\n\n\tprivate static ensureDefaultLevel(options: LoggerLevelOptions | undefined, defaults: LoggerLevelOptions, color: Color, name: string) {\n\t\tif (options) return new LoggerLevel(options);\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 ? `${color(name.padEnd(5, ' '))} - ` : ''\n\t\t});\n\t}\n}\n\n/**\n * The logger options.\n * @since 1.0.0\n */\nexport interface LoggerOptions {\n\t/**\n\t * A writable stream for the output logs.\n\t * @since 1.0.0\n\t * @default process.stdout\n\t */\n\tstdout?: NodeJS.WritableStream;\n\n\t/**\n\t * A writable stream for the error logs.\n\t * @since 1.0.0\n\t * @default process.stderr\n\t */\n\tstderr?: NodeJS.WritableStream;\n\n\t/**\n\t * The default options used to fill all the possible values for {@link LoggerOptions.format}.\n\t * @since 1.0.0\n\t * @default options.format.none ?? {}\n\t */\n\tdefaultFormat?: LoggerLevelOptions;\n\n\t/**\n\t * The options for each log level. LogLevel.None serves to set the default for all keys, where only\n\t * {@link LoggerTimestampOptions.timestamp} and {@link LoggerLevelOptions.prefix} would be overridden.\n\t * @since 1.0.0\n\t * @default {}\n\t */\n\tformat?: LoggerFormatOptions;\n\n\t/**\n\t * The minimum log level.\n\t * @since 1.0.0\n\t * @default LogLevel.Info\n\t */\n\tlevel?: LogLevel;\n\n\t/**\n\t * The string that joins different messages.\n\t * @since 1.0.0\n\t * @default ' '\n\t */\n\tjoin?: string;\n\n\t/**\n\t * The inspect depth when logging objects.\n\t * @since 1.0.0\n\t * @default 0\n\t */\n\tdepth?: number;\n}\n\n/**\n * The logger format options.\n * @since 1.0.0\n */\nexport interface LoggerFormatOptions {\n\t/**\n\t * The logger options for the lowest log level, used when calling {@link ILogger.trace}.\n\t * @since 1.0.0\n\t */\n\ttrace?: LoggerLevelOptions;\n\n\t/**\n\t * The logger options for the debug level, used when calling {@link ILogger.debug}.\n\t * @since 1.0.0\n\t */\n\tdebug?: LoggerLevelOptions;\n\n\t/**\n\t * The logger options for the info level, used when calling {@link ILogger.info}.\n\t * @since 1.0.0\n\t */\n\tinfo?: LoggerLevelOptions;\n\n\t/**\n\t * The logger options for the warning level, used when calling {@link ILogger.warn}.\n\t * @since 1.0.0\n\t */\n\twarn?: LoggerLevelOptions;\n\n\t/**\n\t * The logger options for the error level, used when calling {@link ILogger.error}.\n\t * @since 1.0.0\n\t */\n\terror?: LoggerLevelOptions;\n\n\t/**\n\t * The logger options for the critical level, used when calling {@link ILogger.fatal}.\n\t * @since 1.0.0\n\t */\n\tfatal?: LoggerLevelOptions;\n\n\t/**\n\t * The logger options for an unknown or uncategorised level.\n\t * @since 1.0.0\n\t */\n\tnone?: LoggerLevelOptions;\n}\n"]}