diff --git a/lib/logging.js b/lib/logging.js index 4ddfcd0..0240598 100644 --- a/lib/logging.js +++ b/lib/logging.js @@ -4,23 +4,21 @@ class Logger { /** Logger * * @param {{ - * output: NodeJS.WritableStream, - * output2: NodeJS.WritableStream, + * output: NodeJS.WritableStream | {exclusivity: [level: string], stream: NodeJS.WritableStream, noColors: boolean}, + * outputs: NodeJS.WritableStream[] | Array<{exclusivity: [level: string], stream: NodeJS.WritableStream, noColors: boolean}>, * template: string, * levelsColors: {[level: string]: string}, * historySize: number, * onHistoryFull: () => void, * argsPlaceholder: string, * defaultLevel: string, - * dict: Map, - * logFrom: 'debug' | 'info' | 'warn' | 'error' | 'fatal' - * logFrom2: 'debug' | 'info' | 'warn' | 'error' | 'fatal' + * dict: Map * }} options */ constructor(options = {}) { this.output = options.output || process.stdout; - this.output2 = options.output2 || null; + this.outputs = options.outputs || []; this.template = options.template || "[{level} {timestamp}] {message}"; this.levelsColors = options.levelsColors || { // no same colors debug: "\x1b[32m", @@ -39,12 +37,6 @@ class Logger { this.alog("warn;", "History is full, dropping 25 oldest entries."); 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 @@ -98,11 +90,30 @@ class Logger { if (this.levelsColors && this.levelsColors[level]) msg = this.levelsColors[level] + msg + "\x1b[0m"; - //log (if important enough) - if (this.logImportance[level.charAt(0).toUpperCase()] >= this.logImportance[this.logFrom]) - this.output.write(msg + "\n"); - if (this.output2 && this.logImportance[level.charAt(0).toUpperCase()] >= this.logImportance[this.logFrom2]) - this.output2.write(msgNoColors + "\n"); + //log to output + if (this.output.stream) + { + if (this.output.exclusivity) + { + 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 this.history.push({