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

Do Not Concatenate Error Message When An Error Stack is Present #1664

Open
wants to merge 3 commits 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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion lib/winston/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 12 additions & 8 deletions test/formats/errors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,11 @@ describe('format.errors (integration)', function () {
});

it('logger.log(level, msg, meta<error>)', (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();
Expand All @@ -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<error>) [custom error properties]', (done) => {
Expand All @@ -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'
Expand All @@ -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.<level>(error)', (done) => {
Expand Down Expand Up @@ -198,9 +200,11 @@ describe('format.errors (integration)', function () {
});

it('logger.<level>(msg, meta<error>)', (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();
Expand All @@ -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.<level>(msg, meta<error>) [custom error properties]', (done) => {
Expand All @@ -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'
Expand All @@ -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.<level>)`, (done) => {
Expand Down