Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Winston logger names

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.

like image 245
BenR Avatar asked Sep 02 '12 17:09

BenR


People also ask

What is a Winston logger?

winston is designed to be a simple and universal logging library with support for multiple transports. A transport is essentially a storage device for your logs. Each winston logger can have multiple transports (see: Transports) configured at different levels (see: Logging levels).

What is Winston logger in node JS?

Winston is the most popular logging library for Node. js. It aims to make logging more flexible and extensible by decoupling different aspects such as log levels, formatting, and storage so that each API is independent and many combinations are supported. It also uses Node.

Which method is used to create your own logger with Winston?

Creating the Custom Logger classcreateLogger() method. If you wish to completely change the format of the logs you could refer to Winston docs.

What is transport Winston?

In winston a transport is essentially a storage device for your logs. Each instance of a winston logger can have multiple transports configured at different levels. For example, one may want error logs to be stored in a persistent remote location (like a database), but all logs output to the console or a local file.


1 Answers

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
like image 150
darsh Avatar answered Sep 19 '22 06:09

darsh