Winston logger names

2019-06-19 08:21发布

问题:

Flatiron's logging library Winston is quite useful, but I can not figure how to assign names to loggers. I am expecting an output similar to other logging libraries, for instance:

 [<date>] [<log level>] <logger name> - <the message>

Is it possible to configure Winston in such a way?

Thank you.

回答1:

When creating a logging transport, you can provide a label parameter which will be added to the log output between the log level and the log message. For example:

var logger = new (winston.Logger)({
    transports: [
        new (winston.transports.Console)({
            colorize: true,
            prettyPrint: true,
            timestamp: true,
            label: 'CustomLabel'
        })
    ]
});

This will result in the following output:

2016-09-06T12:16:17.335Z - info: [CustomLabel] hello


回答2:

You can name loggers; however, there doesn't appear to be a way to add the logger name to the output.

But I would use this too, if it was available. There is an open pull request that would allow this by adding a 'name' to the output string, so hopefully that comes through.



回答3:

https://github.com/mcclellanmj/SimplyLog goes on the premise of being able to code your own appenders with custom output easily and is centered around named loggers.