Combine child metadata with additional metadata
See original GitHub issueI’m not sure this is a bug or feature request. I’ll use the bug template since it also contains the behavior I’d expect.
Please tell us about your environment:
winston
version?-
winston@2
-
winston@3
-
node -v
outputs: v12.14.0- Operating System? (Windows, macOS, or Linux) Linux
- Language? (all | TypeScript X.X | ES6/7 | ES5 | Dart) ES6/7
What is the problem?
There seems to be no way to combine child metadata with additional properties. In fact, I find the multiple api signatures in which one can log a message to be quite conflicting/confusing. For example, consider this code:
const winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.printf(({ message, prefix}) => `[${prefix}]: ${message}`),
defaultMeta: { prefix: 'root' },
transports: [new winston.transports.Console()],
});
logger.info('foo 1', { foo: 'bar' });
logger.log('info','foo 2');
logger.log('info','foo 3', {});
logger.log({ level: 'info', message: 'foo 4' });
const childLogger = logger.child({ prefix: 'child' });
childLogger.info('foo 5', { foo: 'bar' });
childLogger.log('info','foo 6');
childLogger.log('info','foo 7', {});
childLogger.log({ level: 'info', message: 'foo 8' });
All of these are valid, but produce different/unexpected results:
[root]: foo 1
[undefined]: foo 2
[root]: foo 3
[root]: foo 4
[root]: foo 5
[child]: foo 6
[root]: foo 7
[root]: foo 8
What do you expect to happen instead?
I can’t really find an api reference, so it’s hard to know what is to be expected and what not. But I would expect that when I create a child logger with metadata, the metadata acts in the way the default metadata on the logger works. Because now, there is no way to add in additional properties in a log call, without losing the metadata passed in to the child logger.
So I would expect that I can still call childLogger.log('info', 'message', { additionalProp: 'foo' })
and still have the prefix=child
property in the metadata. And in fact, I would expect this to be the case for any of the example calls to childLogger.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:5 (2 by maintainers)
Top GitHub Comments
@aressler38 I took a more radical and seemingly consistent approach in #1879. Reused your test though.
Closing as this issue is being consolidated into #2029