From de3e95c383eba35c456836cbdb08aa90b9ce7301 Mon Sep 17 00:00:00 2001 From: faulty Date: Mon, 2 May 2022 19:31:57 +0200 Subject: [PATCH] idkanymore --- .gitignore | 1 + lib/logging.js | 46 ++++++++++++++++++++++++++++------------------ 2 files changed, 29 insertions(+), 18 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/lib/logging.js b/lib/logging.js index ce3822d..c0c0f35 100644 --- a/lib/logging.js +++ b/lib/logging.js @@ -19,6 +19,7 @@ class Logger { debug: "\x1b[32m", info: "\x1b[36m", warn: "\x1b[33m", + "warn;": "\x1b[33m", error: "\x1b[31m", fatal: "\x1b[35m" }; @@ -28,7 +29,8 @@ class Logger { this.history = []; this.historySize = options.historySize || 100; this.onHistoryFull = options.onHistoryFull || function() { - // TODO: implement + this.alog("warn;", "History is full, dropping 25 oldest entries."); + this.history = this.history.slice(25); }; } @@ -39,9 +41,7 @@ class Logger { * */ log (message, ...args) - { - this.alog(this.defaultLevel, message, ...args); - } + {this.alog(this.defaultLevel, message, args);} /** alog * @@ -50,35 +50,45 @@ class Logger { * @param {any} ...args * */ - alog(level, message, ...args) + alog(level, message, args = []) { //replace placeholders with args let msg = this.template; - msg = msg.replace(/\{timestamp\}/g, new Date().toISOString()); msg = msg.replace(/\{level\}/g, level.charAt(0).toUpperCase()); + // [I {timestamp}] {message} + msg = msg.replace(/\{timestamp\}/g, new Date().toISOString()); + // [I 1/1/1970 00:00:00] {message} msg = msg.replace(/\{message\}/g, message); + // [I 1/1/1970 00:00:00] spul {} {} //replace placeholders with args let argPlaceholder = this.argsPlaceholder; - let regex = new RegExp(argPlaceholder, "g"); - let stringArgCount = (regex.exec(msg) || []).length; + let matches = (msg.match(new RegExp(argPlaceholder, "g")) || []).length; + let argsFilled = 0; args.forEach((v, i, a) => { if (Buffer.isBuffer(v)) - { v = conversion.properHex(v).str; - } if (typeof v === 'object') - { - v = JSON.stringify(v); - } + v = "\x1b[0m" + JSON.stringify(v, null, 2) + "\x1b[0m"; msg = msg.replace(argPlaceholder, v); + argsFilled++; + console.log(`${argPlaceholder} -> ${v}`); }); - if (stringArgCount < args.length) - msg += ` (${args.length - stringArgCount} args overflow.)`; - else if (stringArgCount > args.length) - msg += ` (${stringArgCount - args.length} args missing.)`; + + if (matches !== argsFilled && args.length >= matches) + console.log("warn;", "Not all placeholders were replaced.\n" + + "Placeholders: " + matches + "\n" + + "Args: " + args.length + "\n" + + "Args filled: " + argsFilled + "\n" + + "Message: " + msg); + + + if (matches < args.length) + msg += ` (${args.length - matches} args overflow.)`; + else if (matches > args.length) + msg += ` (${matches - args.length} args missing.)`; //replace dictionary keys if (this.dict) @@ -96,7 +106,7 @@ class Logger { message: message, args: args }); - if (this.history.length >= this.historySize) + if (this.history.length >= this.historySize && level !== "warn;") this.onHistoryFull(); } }