diff --git a/README.md b/README.md index 53cf4f47b..2043e9737 100644 --- a/README.md +++ b/README.md @@ -280,13 +280,16 @@ console.log(SPLAT === Symbol.for('splat')); ``` > **NOTE:** any `{ messageĀ }` property in a `meta` object provided will -> automatically be concatenated to any `msg` already provided: For +> automatically be concatenated to any `message` already provided. For > example the below will concatenate 'world' onto 'hello': > > ``` js > logger.log('error', 'hello', { message: 'world' }); > logger.info('hello', { message: 'world' }); > ``` +> +> The `{ message }` property of an `Error` with a `stack` will *not* be +> concatenated to the previous `message`. ## Formats diff --git a/lib/winston/logger.js b/lib/winston/logger.js index 4f0095c25..a12d4c588 100644 --- a/lib/winston/logger.js +++ b/lib/winston/logger.js @@ -238,8 +238,9 @@ class Logger extends Transform { message: msg }); - if (meta.message) info.message += `${meta.message}`; + // Do not modify message if error stack is available. if (meta.stack) info.stack = meta.stack; + else if (meta.message) info.message += `${meta.message}`; this.write(info); return this; diff --git a/test/formats/errors.test.js b/test/formats/errors.test.js index 0364a469a..afcf4c78a 100644 --- a/test/formats/errors.test.js +++ b/test/formats/errors.test.js @@ -99,9 +99,11 @@ describe('format.errors (integration)', function () { }); it('logger.log(level, msg, meta)', (done) => { + const err = new Error('Errors lack .toJSON() lulz'); const logger = helpers.createLogger(function (info) { assumeExpectedInfo(info, { - message: 'Caught error: Errors lack .toJSON() lulz' + message: 'Caught error', + stack: err.stack }); done(); @@ -110,7 +112,7 @@ describe('format.errors (integration)', function () { format.printf(info => info.message) )); - logger.log('info', 'Caught error: ', new Error('Errors lack .toJSON() lulz')); + logger.log('info', 'Caught error', err); }); it('logger.log(level, msg, meta) [custom error properties]', (done) => { @@ -120,7 +122,7 @@ describe('format.errors (integration)', function () { const logger = helpers.createLogger(function (info) { assumeExpectedInfo(info, { - message: 'Caught error: Errors lack .toJSON() lulz', + message: 'Caught error', stack: err.stack, something: true, wut: 'another string' @@ -132,7 +134,7 @@ describe('format.errors (integration)', function () { format.printf(info => info.message) )); - logger.log('info', 'Caught error: ', err); + logger.log('info', 'Caught error', err); }); it('logger.(error)', (done) => { @@ -198,9 +200,11 @@ describe('format.errors (integration)', function () { }); it('logger.(msg, meta)', (done) => { + const err = new Error('Errors lack .toJSON() lulz'); const logger = helpers.createLogger(function (info) { assumeExpectedInfo(info, { - message: 'Caught error: Errors lack .toJSON() lulz' + message: 'Caught error', + stack: err.stack }); done(); @@ -209,7 +213,7 @@ describe('format.errors (integration)', function () { format.printf(info => info.message) )); - logger.info('Caught error: ', new Error('Errors lack .toJSON() lulz')); + logger.info('Caught error', err); }); it('logger.(msg, meta) [custom error properties]', (done) => { @@ -219,7 +223,7 @@ describe('format.errors (integration)', function () { const logger = helpers.createLogger(function (info) { assumeExpectedInfo(info, { - message: 'Caught error: Errors lack .toJSON() lulz', + message: 'Caught error', stack: err.stack, something: true, wut: 'another string' @@ -231,7 +235,7 @@ describe('format.errors (integration)', function () { format.printf(info => info.message) )); - logger.info('Caught error: ', err); + logger.info('Caught error', err); }); it(`Promise.reject().catch(logger.)`, (done) => {