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

Don't change behavior when message matches splat regex. #1710

Closed
wants to merge 2 commits into from
Closed
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
37 changes: 12 additions & 25 deletions lib/winston/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ const Profiler = require('./profiler');
const { warn } = require('./common');
const config = require('./config');

/**
* Captures the number of format (i.e. %s strings) in a given string.
* Based on `util.format`, see Node.js source:
* https://github.com/nodejs/node/blob/b1c8f15c5f169e021f7c46eb7b219de95fe97603/lib/util.js#L201-L230
* @type {RegExp}
*/
const formatRegExp = /%[scdjifoO%]/g;

/**
* TODO: add class description.
* @type {Logger}
Expand Down Expand Up @@ -226,26 +218,21 @@ class Logger extends Transform {
return this;
}

const [meta] = splat;
// if the last argument is an object, treat it as meta and merge it into info
const [meta] = splat.slice(-1);
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
});
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;
if (meta.message) info.message = `${info.message} ${meta.message}`;
if (meta.stack) info.stack = meta.stack;

this.write(info);
return this;
}
this.write(info);
return this;
}

this.write(Object.assign({}, this.defaultMeta, {
Expand Down
16 changes: 15 additions & 1 deletion test/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -788,15 +788,29 @@ describe('Logger (logging exotic data types)', function () {
const logged = [];
const logger = helpers.createLogger(function (info, enc, next) {
logged.push(info);
assume(info.extra).equals(true);
assume(info.label).equals(undefined);
next();

if (logged.length === 1) done();
});
}, format.splat());

logger.info('Hello %j', { label: 'world' }, { extra: true });
});

it(`.info('Literal "%c"') does not drop meta props when splat formatter not used`, function (done) {
const logged = [];
const logger = helpers.createLogger(function (info, enc, next) {
logged.push(info);
assume(info.label).equals('world');
next();

if (logged.length === 1) done();
});

logger.info('Literal "%c"', { label: 'world' });
});

it(`.info('Hello') and .info('Hello %d') preserve meta with splat format`, function (done) {
const logged = [];
const logger = helpers.createLogger(function (info, enc, next) {
Expand Down