Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain use of levels in winston logger

Hey i am using this winston logger, kindly explain use of level inside the transports, what will happen if i use logger with info while logging, do i have to use debug while i log my data.

var logger = new (winston.Logger)({
transports: [
  new (winston.transports.Console)({
    level: 'debug',
    json: true
  }),
  new (winston.transports.File)({
    name: 'order_check',
    filename: './logs/order_check.log',
    level: 'debug'
  })
]
});
logger.log("info","request body");
like image 728
Vikarn Singh Mankotia Avatar asked Jan 15 '16 10:01

Vikarn Singh Mankotia


People also ask

What is level in logger?

A log level or log severity is a piece of information telling how important a given log message is. It is a simple, yet very powerful way of distinguishing log events from each other. If the log levels are used properly in your application all you need is to look at the severity first.

What are the different log levels which can be used for logging?

Logging levels explained. The most common logging levels include FATAL, ERROR, WARN, INFO, DEBUG, TRACE, ALL, and OFF.

How many log levels does Winston support?

'winston' by default uses npm logging levels. Here the severity of all levels is prioritized from the most important to least important from 0 to 6 (highest to lowest).

How do I choose a logging level?

When choosing a log level, it's important to know how visible you want the message to be, how big of a problem it is, and what you want the user to do about it. With that in mind, this is the decision tree I follow when choosing a log level: Can you continue execution after this? If no, use the error log level.


1 Answers

The level inside your transport indiciates the minimum logging level that transport will "listen out for"

From the documentation: https://github.com/winstonjs/winston#logging-levels

Each level is given a specific integer priority. The higher the priority the more important the message is considered to be

{ error: 0, warn: 1, info: 2, verbose: 3, debug: 4, silly: 5 }

So, in your example, you're transports are configured for debug: 4

This means, it will log levels

  • 4 (debug)
  • 3 (verbose)
  • 2 (info)
  • 1 (warn)
  • 0 (error)

A good use case for this would be to set one transport (Console for example) to debug, and your other to info.

This would output all debug information to the console, but only log info to file, preventing log file clutter.

like image 113
Alex Avatar answered Oct 24 '22 09:10

Alex