How to set log level in Winston/Node.js

Silent User picture Silent User · Mar 20, 2013 · Viewed 36.9k times · Source

I am using Winston logging with my Node.js app and have defined a file transport. Throughout my code, I log using either logger.error, logger.warn, or logger.info.

My question is, how do I specify the log level? Is there a config file and value that I can set so that only the appropriate log messages are logged? For example, I'd like the log level to be "info" in my development environment but "error" in production.

Answer

AndreasPizsa picture AndreasPizsa · Apr 8, 2014

If you are using the default logger, you can adjust the log levels like this:

const winston = require('winston');
// ...
winston.level = 'debug';

will set the log level to 'debug'. (Tested with winston 0.7.3, default logger is still around in 3.2.1).

However, the documentation recommends creating a new logger with the appropriate log levels and then using that logger:

const myLogger = winston.createLogger({
  level: 'debug'
});
myLogger.debug('hello world');

If you are already using the default logger in your code base this may require you to replace all usages with this new logger that you are using:

const winston = require('winston');
// default logger
winston.log('debug', 'default logger being used');

// custom logger
myLogger.log('debug', 'custom logger being used');