question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Overriding format elements in child logger?

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:9
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
E5-Aaroncommented, Jul 27, 2022

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' });
0reactions
maverick1872commented, Jul 27, 2022

@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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Logging Cookbook — Python 3.11.1 documentation
ERROR) # create formatter and add it to the handlers formatter = logging. ... you can use an alternative serialization scheme by overriding...
Read more >
Complete Winston Logger Guide With Hands-on Examples
Whereas, the Winston logger allows you to overwrite, and customize, ... Winston uses logform to handle the log-formatting aspects.
Read more >
Java util logging wrong format after overriding the formatter
The logger tree changes over time when different parts of your code requests new loggers. So it matters when you call this method....
Read more >
logging-format-interpolation / W1202 - Pylint 2.16.0-dev ...
<logging method>(format_string.format(format_args...))". Use another type of string formatting instead. You can use % formatting but leave interpolation to the ...
Read more >
Logger | NestJS - A progressive Node.js framework
Logger · disable logging entirely · specify the log level of detail (e.g., display errors, warnings, debug information, etc.) · override timestamp ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found