-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Consistent handling of meta with (and without) interpolation in winst…
…on & logform (#1552) * [doc fix] Build on the work from #1485 fixes. * [tiny] Formatting changes. * [fix test] Updated tests to latest (consistent) semantics. * [dist] Update to `logform@2.0.0` and `winston-transport@4.3.0`. * [doc] Document all possible permuations for splat.
- Loading branch information
Showing
6 changed files
with
286 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
const winston = require('../'); | ||
let { format } = winston; | ||
|
||
/* | ||
* Simple helper for stringifying all remaining | ||
* properties. | ||
*/ | ||
function rest(info) { | ||
return JSON.stringify(Object.assign({}, info, { | ||
level: undefined, | ||
message: undefined, | ||
splat: undefined, | ||
label: undefined | ||
})); | ||
} | ||
|
||
let logger = winston.createLogger({ | ||
transports: [new winston.transports.Console({ level: 'info' })], | ||
format: format.combine( | ||
format.splat(), | ||
format.printf(info => `[${info.label}] ${info.message} ${rest(info)}`) | ||
) | ||
}); | ||
|
||
logger.log( | ||
'info', | ||
'any message', | ||
{ | ||
label: 'label!', | ||
extra: true | ||
} | ||
); | ||
|
||
logger.log( | ||
'info', | ||
'let\'s %s some %s', | ||
'interpolate', | ||
'splat parameters', | ||
{ | ||
label: 'label!', | ||
extra: true | ||
} | ||
); | ||
|
||
logger.log( | ||
'info', | ||
'first is a string %s [[%j]]', | ||
'behold a string', | ||
{ beAware: 'this will interpolate' }, | ||
{ | ||
label: 'label!', | ||
extra: true | ||
} | ||
); | ||
|
||
logger.log( | ||
'info', | ||
'first is an object [[%j]]', | ||
{ beAware: 'this will interpolate' }, | ||
{ | ||
label: 'label!', | ||
extra: true | ||
} | ||
); | ||
|
||
// | ||
// Non-enumerable properties (such as "message" and "stack" in Error | ||
// instances) will not be merged into any `info`. | ||
// | ||
const terr = new Error('lol please stop doing this'); | ||
terr.label = 'error'; | ||
terr.extra = true; | ||
logger.log( | ||
'info', | ||
'any message', | ||
terr | ||
); | ||
|
||
logger.log( | ||
'info', | ||
'let\'s %s some %s', | ||
'interpolate', | ||
'splat parameters', | ||
terr | ||
); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.