generated from sapphiredev/sapphire-template
/
Logger.ts
205 lines (177 loc) · 5.53 KB
/
Logger.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { Logger as BuiltinLogger, LogLevel, type LogMethods } from '@sapphire/framework';
import { bgRed, cyan, gray, isColorSupported, magenta, red, white, yellow, type Color } from 'colorette';
import { Console } from 'console';
import { inspect, type InspectOptions } from 'util';
import { LoggerLevel, type LoggerLevelOptions } from './LoggerLevel';
/**
* The logger class.
* @since 1.0.0
*/
export class Logger extends BuiltinLogger {
/**
* The console this writes to.
* @since 1.0.0
*/
public readonly console: Console;
/**
* The formats supported by the logger.
* @since 1.0.0
*/
public readonly formats: Map<LogLevel, LoggerLevel>;
/**
* The string `write` will join values by.
* @since 1.0.0
*/
public readonly join: string;
/**
* The inspect depth when logging objects.
* @since 1.0.0
*/
public readonly depth: number;
public constructor(options: LoggerOptions = {}) {
super(options.level ?? LogLevel.Info);
this.console = new Console(options.stdout ?? process.stdout, options.stderr ?? process.stderr);
this.formats = Logger.createFormatMap(options.format, options.defaultFormat);
this.join = options.join ?? ' ';
this.depth = options.depth ?? 0;
}
/**
* Writes the log message given a level and the value(s).
* @param level The log level.
* @param values The values to log.
*/
public override write(level: LogLevel, ...values: readonly unknown[]): void {
if (level < this.level) return;
const method = this.levels.get(level) ?? 'log';
const formatter = this.formats.get(level) ?? this.formats.get(LogLevel.None)!;
this.console[method](formatter.run(this.preprocess(values)));
}
/**
* Pre-processes an array of values.
* @since 1.0.0
* @param values The values to pre-process.
*/
protected preprocess(values: readonly unknown[]) {
const inspectOptions: InspectOptions = { colors: isColorSupported, depth: this.depth };
return values.map((value) => (typeof value === 'string' ? value : inspect(value, inspectOptions))).join(this.join);
}
private get levels() {
return Reflect.get(BuiltinLogger, 'levels') as Map<LogLevel, LogMethods>;
}
/**
* Gets whether or not colorette is enabled.
* @since 1.0.0
*/
public static get stylize() {
return isColorSupported;
}
private static createFormatMap(options: LoggerFormatOptions = {}, defaults: LoggerLevelOptions = options.none ?? {}) {
return new Map<LogLevel, LoggerLevel>([
[LogLevel.Trace, Logger.ensureDefaultLevel(options.trace, defaults, gray, 'TRACE')],
[LogLevel.Debug, Logger.ensureDefaultLevel(options.debug, defaults, magenta, 'DEBUG')],
[LogLevel.Info, Logger.ensureDefaultLevel(options.info, defaults, cyan, 'INFO')],
[LogLevel.Warn, Logger.ensureDefaultLevel(options.warn, defaults, yellow, 'WARN')],
[LogLevel.Error, Logger.ensureDefaultLevel(options.error, defaults, red, 'ERROR')],
[LogLevel.Fatal, Logger.ensureDefaultLevel(options.fatal, defaults, bgRed, 'FATAL')],
[LogLevel.None, Logger.ensureDefaultLevel(options.none, defaults, white, '')]
]);
}
private static ensureDefaultLevel(options: LoggerLevelOptions | undefined, defaults: LoggerLevelOptions, color: Color, name: string) {
if (options) return new LoggerLevel(options);
return new LoggerLevel({
...defaults,
timestamp: defaults.timestamp === null ? null : { ...(defaults.timestamp ?? {}), color },
infix: name.length ? `${color(name.padEnd(5, ' '))} - ` : ''
});
}
}
/**
* The logger options.
* @since 1.0.0
*/
export interface LoggerOptions {
/**
* A writable stream for the output logs.
* @since 1.0.0
* @default process.stdout
*/
stdout?: NodeJS.WritableStream;
/**
* A writable stream for the error logs.
* @since 1.0.0
* @default process.stderr
*/
stderr?: NodeJS.WritableStream;
/**
* The default options used to fill all the possible values for {@link LoggerOptions.format}.
* @since 1.0.0
* @default options.format.none ?? {}
*/
defaultFormat?: LoggerLevelOptions;
/**
* The options for each log level. LogLevel.None serves to set the default for all keys, where only
* {@link LoggerTimestampOptions.timestamp} and {@link LoggerLevelOptions.prefix} would be overridden.
* @since 1.0.0
* @default {}
*/
format?: LoggerFormatOptions;
/**
* The minimum log level.
* @since 1.0.0
* @default LogLevel.Info
*/
level?: LogLevel;
/**
* The string that joins different messages.
* @since 1.0.0
* @default ' '
*/
join?: string;
/**
* The inspect depth when logging objects.
* @since 1.0.0
* @default 0
*/
depth?: number;
}
/**
* The logger format options.
* @since 1.0.0
*/
export interface LoggerFormatOptions {
/**
* The logger options for the lowest log level, used when calling {@link ILogger.trace}.
* @since 1.0.0
*/
trace?: LoggerLevelOptions;
/**
* The logger options for the debug level, used when calling {@link ILogger.debug}.
* @since 1.0.0
*/
debug?: LoggerLevelOptions;
/**
* The logger options for the info level, used when calling {@link ILogger.info}.
* @since 1.0.0
*/
info?: LoggerLevelOptions;
/**
* The logger options for the warning level, used when calling {@link ILogger.warn}.
* @since 1.0.0
*/
warn?: LoggerLevelOptions;
/**
* The logger options for the error level, used when calling {@link ILogger.error}.
* @since 1.0.0
*/
error?: LoggerLevelOptions;
/**
* The logger options for the critical level, used when calling {@link ILogger.fatal}.
* @since 1.0.0
*/
fatal?: LoggerLevelOptions;
/**
* The logger options for an unknown or uncategorised level.
* @since 1.0.0
*/
none?: LoggerLevelOptions;
}