Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An option to exclude spat from message #2323

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ declare namespace winston {
handleRejections?: boolean;
exceptionHandlers?: any;
rejectionHandlers?: any;
excludeSplatFromMessage?: boolean;
}

class Logger extends NodeJSStream.Transform {
Expand Down
45 changes: 25 additions & 20 deletions lib/winston/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -147,6 +148,8 @@ class Logger extends Transform {
if (rejectionHandlers) {
this.rejections.handle(rejectionHandlers);
}

this.excludeSplatFromMessage = !!excludeSplatFromMessage;
}

isLevelEnabled(level) {
Expand Down Expand Up @@ -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;
}
}
}

Expand Down