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": {