From 9e38f0b3e35347c2282b4ddc7c28ca2e61551211 Mon Sep 17 00:00:00 2001 From: faulty Date: Mon, 2 May 2022 19:41:46 +0200 Subject: [PATCH] Fixed it!! --- lib/logging.js | 24 +++++++++++++++++++----- package.json | 2 +- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/logging.js b/lib/logging.js index c0c0f35..d5c6648 100644 --- a/lib/logging.js +++ b/lib/logging.js @@ -66,17 +66,31 @@ class Logger { let argPlaceholder = this.argsPlaceholder; let matches = (msg.match(new RegExp(argPlaceholder, "g")) || []).length; let argsFilled = 0; - args.forEach((v, i, a) => { + // args.forEach((v, i, a) => { + // if (Buffer.isBuffer(v)) + // v = conversion.properHex(v).str; + // if (typeof v === 'object') + // v = "\x1b[0m" + JSON.stringify(v, null, 2) + "\x1b[0m"; + + // msg = msg.replace(argPlaceholder, v); + // argsFilled++; + // console.log(`${argPlaceholder} -> ${v}`); + // }); + + // regexp replace + let regexp = new RegExp(argPlaceholder, "g"); + msg = msg.replace(regexp, (match, offset, string) => { + if (argsFilled >= matches) + return match; + let v = args[argsFilled]; if (Buffer.isBuffer(v)) v = conversion.properHex(v).str; if (typeof v === 'object') v = "\x1b[0m" + JSON.stringify(v, null, 2) + "\x1b[0m"; - - msg = msg.replace(argPlaceholder, v); argsFilled++; - console.log(`${argPlaceholder} -> ${v}`); + return v; }); - + if (matches !== argsFilled && args.length >= matches) console.log("warn;", "Not all placeholders were replaced.\n" + "Placeholders: " + matches + "\n" + diff --git a/package.json b/package.json index b5b8d88..3eb5491 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "logger4njs", - "version": "1.0.2", + "version": "1.0.3", "description": "Logger for Node JS", "main": "index.js", "directories": {