Overriding format elements in child logger?
See original GitHub issueHello
Try as a I might I can’t get a child logger to override format properties in a parent logger. Ideally what I would like to do is have a parent logger set all of the format properties but the label and then set that in a child logger (which I will vend on a per class basis). But I find i can’t even set the entire format object at the child property, let alone override properties in a parent logger. Am wondering whether I simply have the wrong end of the stick with child loggers or whether this is a bug?
The basic root looks like this:
this.#rootLogger = winston.createLogger({
levels: this.#winstonLogLevels,
level: 'DEBUG',
transports: [
new winston.transports.File({ filename: filename }),
new winston.transports.Console()
]
});
if I define a child logger like this:
const childLogger = this.#rootLogger({
format: winston.format.combine(
winston.format.timestamp(),
winston.format.label({ label: "WillNeverShow" }),
this.#stdFormat);
with:
#stdFormat = winston.format.printf((info) => {
return `${info.timestamp} [${info.label}] ${info.level}: ${info.message}`;
});
and then I log with this logger, it won’t print out the timestamp or the label ( it will print out undefined for both)
Even if I define the format in the rootLogger, like this:
this.#rootLogger = winston.createLogger({
levels: this.#winstonLogLevels,
level: 'DEBUG',
format: winston.format.combine(
winston.format.timestamp(),
this.#stdFormat),
transports: [
new winston.transports.File({ filename: filename }),
new winston.transports.Console()
]
});
Then I still can’t get a child logger to show a label, not matter how i try to combine etc.
Is this bug? Or am I just not “doing it right”?
Thanks
Issue Analytics
- State:
- Created 4 years ago
- Reactions:9
- Comments:6 (3 by maintainers)
Top GitHub Comments
I figured out the way the meta object was merged in the child is “up” a level compared to the initial configuration of the logger. So, I abandoned use of the ‘label’ formatter in the parent entirely, and used a printf formatter that uses a label element instead.
Then I can override the label in a child logger via
@E5-Aaron I am not familiar with the implementation of the label format. Although I don’t believe it’s intended to behave in the manner of which you’re desiring.
https://github.com/winstonjs/logform#label