Skip to content

Commit a910c28

Browse files
authored
[fix] Better handling of new Error(string) throughout the pipeline(s). Fixes #1338, #1486 (#1562)
* [tiny dist] Whitespace. package-lock.json * [test] Add E2E integration tests with logform `errors` format. * [test] 5:00pm. Press return. * [fix test] More E2E errors coverage. * [test] Test custom error properties. * [tiny doc] Make note of duplicate coverage in `logger`. Update minor formatting. * [test] All E2E tests work except for one... * [fix test doc] All 14 variations of handling `new Error()` now work as most folks expect. * [tiny] Fix up file header. * [dist] Bump to `logform@2.1.0` * [fix tiny] Whitespace. * s/req_id/requestId/ * [fix test] Address PR comments. Add test coverage for defaultMeta over .child(additionalMeta)
1 parent 0da77d4 commit a910c28

8 files changed

Lines changed: 528 additions & 223 deletions

File tree

README.md

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ const logger = winston.createLogger({
203203
]
204204
});
205205

206-
const childLogger = logger.child({ req_id: '451' });
206+
const childLogger = logger.child({ requestId: '451' });
207207
```
208208

209209
### Streams, `objectMode`, and `info` objects
@@ -235,19 +235,13 @@ treated as immutable by all code.
235235
- `Symbol.for('message'):` complete string message set by "finalizing
236236
formats": `json`, `logstash`, `printf`, `prettyPrint`, and `simple`.
237237

238-
> **NOTE:** the `message` and `level` properties are considered reserved.
239-
> Please be aware of this when logging additional metadata objects. For
240-
> example the below will suppress the `message` property of the metadata
241-
> provided:
238+
> **NOTE:** any `{ message }` property in a `meta` object provided will
239+
> automatically be concatenated to any `msg` already provided: For
240+
> example the below will concatenate 'world' onto 'hello':
242241
>
243242
> ``` js
244-
> logger.log('hello', { message: 'will be hidden' });
245-
> ```
246-
>
247-
> To work around this use the `splat()` format below. e.g.:
248-
>
249-
> ``` js
250-
> logger.log('hello %j', { message: 'will be shown' });
243+
> logger.log('error', 'hello', { message: 'world' });
244+
> logger.info('hello', { message: 'world' });
251245
> ```
252246
253247
## Formats

examples/errors.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const { createLogger, format, transports } = require('../');
2+
const { combine, errors, json } = format;
3+
4+
const logger = createLogger({
5+
format: combine(
6+
errors({ stack: true }),
7+
json()
8+
),
9+
transports: [
10+
new transports.Console(),
11+
]
12+
});
13+
14+
logger.warn(new Error('Error passed as info'));
15+
logger.log('error', new Error('Error passed as message'));
16+
17+
logger.warn('Maybe important error: ', new Error('Error passed as meta'));
18+
logger.log('error', 'Important error: ', new Error('Error passed as meta'));
19+
20+
logger.error(new Error('Error as info'));

lib/winston/logger.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,12 @@ const formatRegExp = /%[scdjifoO%]/g;
3333
*/
3434
class Logger extends Transform {
3535
/**
36-
* Constructor function for the Logger object responsible for persisting log
37-
* messages and metadata to one or more transports.
38-
* @param {!Object} options - foo
39-
*/
36+
* Constructor function for the Logger object responsible for persisting log
37+
* messages and metadata to one or more transports.
38+
* @param {!Object} options - foo
39+
*/
4040
constructor(options) {
41-
super({
42-
objectMode: true
43-
});
41+
super({ objectMode: true });
4442
this.configure(options);
4543
}
4644

@@ -55,7 +53,12 @@ class Logger extends Transform {
5553
info
5654
);
5755

58-
// Object.assign doesn't copy inherited Error properties so we have to do that explicitly
56+
// Object.assign doesn't copy inherited Error
57+
// properties so we have to do that explicitly
58+
//
59+
// Remark (indexzero): we should remove this
60+
// since the errors format will handle this case.
61+
//
5962
if (info instanceof Error) {
6063
infoClone.stack = info.stack;
6164
infoClone.message = info.message;
@@ -228,29 +231,28 @@ class Logger extends Transform {
228231
const tokens = msg && msg.match && msg.match(formatRegExp);
229232

230233
if (!tokens) {
231-
this.write(Object.assign({}, meta, {
232-
[LEVEL]: level,
233-
[SPLAT]: splat,
234-
level,
235-
message: msg
236-
}, this.defaultMeta));
237-
} else {
238-
this.write(Object.assign({}, {
234+
const info = Object.assign({}, this.defaultMeta, meta, {
239235
[LEVEL]: level,
240236
[SPLAT]: splat,
241237
level,
242238
message: msg
243-
}, this.defaultMeta));
239+
});
240+
241+
if (meta.message) info.message += `${meta.message}`;
242+
if (meta.stack) info.stack = meta.stack;
243+
244+
this.write(info);
245+
return this;
244246
}
245-
} else {
246-
this.write(Object.assign({}, {
247-
[LEVEL]: level,
248-
[SPLAT]: splat,
249-
level,
250-
message: msg
251-
}, this.defaultMeta));
252247
}
253248

249+
this.write(Object.assign({}, this.defaultMeta, {
250+
[LEVEL]: level,
251+
[SPLAT]: splat,
252+
level,
253+
message: msg
254+
}));
255+
254256
return this;
255257
}
256258

0 commit comments

Comments
 (0)