This commit is contained in:
Strix 2022-05-08 02:34:16 +02:00
parent 69e5c1520a
commit 047b161a31
Signed by untrusted user who does not match committer: didier
GPG key ID: 01E71F18AA4398E5

View file

@ -4,23 +4,21 @@ class Logger {
/** Logger /** Logger
* *
* @param {{ * @param {{
* output: NodeJS.WritableStream, * output: NodeJS.WritableStream | {exclusivity: [level: string], stream: NodeJS.WritableStream, noColors: boolean},
* output2: NodeJS.WritableStream, * outputs: NodeJS.WritableStream[] | Array<{exclusivity: [level: string], stream: NodeJS.WritableStream, noColors: boolean}>,
* template: string, * template: string,
* levelsColors: {[level: string]: string}, * levelsColors: {[level: string]: string},
* historySize: number, * historySize: number,
* onHistoryFull: () => void, * onHistoryFull: () => void,
* argsPlaceholder: string, * argsPlaceholder: string,
* defaultLevel: string, * defaultLevel: string,
* dict: Map<string, string>, * dict: Map<string, string>
* logFrom: 'debug' | 'info' | 'warn' | 'error' | 'fatal'
* logFrom2: 'debug' | 'info' | 'warn' | 'error' | 'fatal'
* }} options * }} options
*/ */
constructor(options = {}) constructor(options = {})
{ {
this.output = options.output || process.stdout; this.output = options.output || process.stdout;
this.output2 = options.output2 || null; this.outputs = options.outputs || [];
this.template = options.template || "[{level} {timestamp}] {message}"; this.template = options.template || "[{level} {timestamp}] {message}";
this.levelsColors = options.levelsColors || { // no same colors this.levelsColors = options.levelsColors || { // no same colors
debug: "\x1b[32m", debug: "\x1b[32m",
@ -39,12 +37,6 @@ class Logger {
this.alog("warn;", "History is full, dropping 25 oldest entries."); this.alog("warn;", "History is full, dropping 25 oldest entries.");
this.history = this.history.slice(25); this.history = this.history.slice(25);
}; };
this.logFrom = options.logFrom ? options.logFrom.charAt(0).toUpperCase() : "D"; // when to stop logging to output
this.logFrom2 = options.logFrom2 ? options.logFrom2.charAt(0).toUpperCase() : "D"; // when to stop logging to output2
// Order from least important to most: D, I, W, E, F
this.logImportance = {
D: 0, I: 1, W: 2, E: 3, F: 4
};
} }
/** log /** log
@ -98,11 +90,30 @@ class Logger {
if (this.levelsColors && this.levelsColors[level]) if (this.levelsColors && this.levelsColors[level])
msg = this.levelsColors[level] + msg + "\x1b[0m"; msg = this.levelsColors[level] + msg + "\x1b[0m";
//log (if important enough) //log to output
if (this.logImportance[level.charAt(0).toUpperCase()] >= this.logImportance[this.logFrom]) if (this.output.stream)
this.output.write(msg + "\n"); {
if (this.output2 && this.logImportance[level.charAt(0).toUpperCase()] >= this.logImportance[this.logFrom2]) if (this.output.exclusivity)
this.output2.write(msgNoColors + "\n"); {
if (this.output.exclusivity.includes(level))
this.output.stream.write(this.output.noColors ? msgNoColors : msg);
}
else
this.output.stream.write(this.output.noColors ? msgNoColors : msg);
} else this.output.write(msg + "\n");
//log to outputs
for (let output of this.outputs)
{
if (output.stream) {
if (output.exclusivity)
{
if (output.exclusivity.includes(level))
output.stream.write(output.noColors ? msgNoColors : msg);
} else
output.stream.write(output.noColors ? msgNoColors : msg);
} else output.write(msg + "\n");
}
//save to history //save to history
this.history.push({ this.history.push({