winstonjs / winston

A logger for just about everything.
http://github.com/winstonjs/winston
MIT License
22.86k stars 1.81k forks source link

Overriding format elements in child logger? #1703

Open DullingWine opened 5 years ago

DullingWine commented 5 years ago

Hello

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

SuperKirik commented 3 years ago

Any update on this ?

wbt commented 2 years ago

Is this a duplicate of #1596 and/or addressed by #1989?

maverick1872 commented 2 years ago

Is this a duplicate of #1596 and/or addressed by #1989?

Further review of this issue I don't believe it to be a duplicate of the above metadata issue. Although the root problem is likely to be similar.

E5-Aaron commented 2 years ago

I think I'm running into the exact same thing in 3.8.1. I got excited and thought the main point of labels was that you could override them in children, but nothing seems to happen...

maverick1872 commented 2 years ago

@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

E5-Aaron commented 2 years ago

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

const l = logger.child({ label: 'foo' });
ohroy commented 2 days ago

any news ?