From bc596bfa1acde7f2bbfd5424e986755adfa39fea Mon Sep 17 00:00:00 2001 From: Hossein Zoda Date: Mon, 10 Jul 2023 00:53:10 +0300 Subject: [PATCH] option to exclude spat from message --- index.d.ts | 1 + lib/winston/logger.js | 45 ++++++++++++++++++++++++------------------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/index.d.ts b/index.d.ts index 39e577642..4698ac3a9 100644 --- a/index.d.ts +++ b/index.d.ts @@ -104,6 +104,7 @@ declare namespace winston { handleRejections?: boolean; exceptionHandlers?: any; rejectionHandlers?: any; + excludeSplatFromMessage?: boolean; } class Logger extends NodeJSStream.Transform { diff --git a/lib/winston/logger.js b/lib/winston/logger.js index 5f8758c89..6c5c4960a 100644 --- a/lib/winston/logger.js +++ b/lib/winston/logger.js @@ -93,7 +93,8 @@ class Logger extends Transform { rewriters, stripColors, exceptionHandlers, - rejectionHandlers + rejectionHandlers, + excludeSplatFromMessage } = {}) { // Reset transports if we already have them if (this.transports.length) { @@ -147,6 +148,8 @@ class Logger extends Transform { if (rejectionHandlers) { this.rejections.handle(rejectionHandlers); } + + this.excludeSplatFromMessage = !!excludeSplatFromMessage; } isLevelEnabled(level) { @@ -232,25 +235,27 @@ class Logger extends Transform { return this; } - const [meta] = splat; - if (typeof meta === 'object' && meta !== null) { - // Extract tokens, if none available default to empty array to - // ensure consistancy in expected results - const tokens = msg && msg.match && msg.match(formatRegExp); - - if (!tokens) { - const info = Object.assign({}, this.defaultMeta, meta, { - [LEVEL]: level, - [SPLAT]: splat, - level, - message: msg - }); - - if (meta.message) info.message = `${info.message} ${meta.message}`; - if (meta.stack) info.stack = meta.stack; - - this.write(info); - return this; + if (!this.excludeSplatFromMessage) { + const [meta] = splat; + if (typeof meta === 'object' && meta !== null) { + // Extract tokens, if none available default to empty array to + // ensure consistancy in expected results + const tokens = msg && msg.match && msg.match(formatRegExp); + + if (!tokens) { + const info = Object.assign({}, this.defaultMeta, meta, { + [LEVEL]: level, + [SPLAT]: splat, + level, + message: msg + }); + + if (meta.message) info.message = `${info.message} ${meta.message}`; + if (meta.stack) info.stack = meta.stack; + + this.write(info); + return this; + } } }