How to setup log level in Winston/Node.js?


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

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.

Problem courtesy of: Silent User


Looks like there is a level option in the options passed covered here

From that doc:

var logger = new (winston.Logger)({
  transports: [
    new (winston.transports.Console)({ level: 'error' }),
    new (winston.transports.File)({ filename: 'somefile.log' })

Now, those examples show passing level in the option object to the console transport. When you use a file transport, I believe you would pass an options object that not only contains the filepath but also the level.

That should lead to something like:

var logger = new (winston.Logger)({
  transports: [
    new (winston.transports.File)({ filename: 'somefile.log', level: 'error' })

Per that doc, note also that as of 2.0, it exposes a setLevel method to change at runtime. Look in the Using Log Levels section of that doc.

Solution courtesy of: bryanmac


There are 6 default levels in winston: silly=0(lowest), debug=1, verbose=2, info=3, warn=4, error=5(highest)

While creating the logger transports, you can specify the log level like:

new (winston.transports.File)({ filename: 'somefile.log', level: 'warn' })

Above code will set log level to warn, which means silly, verbose and info will not be output to somefile.log, while warn, debug and error will.

You can also define your own levels:

var myCustomLevels = {
  levels: {
    foo: 0,
    bar: 1,
    baz: 2,
    foobar: 3

var customLevelLogger = new (winston.Logger)({ levels: myCustomLevels.levels });
customLevelLogger.foobar('some foobar level-ed message');

Note that it's better to always include the 6 predefined levels in your own custom levels, in case somewhere used the predefined levels.

Discussion courtesy of: Chen
winston = require('winston');
winston.level = 'debug';

will set the log level to 'debug'. (Tested with winston 0.7.3)

Discussion courtesy of: AndreasPizsa

You can change the logging level in runtime by modifying the level property of the appropriate transport:

var log = new (winston.Logger)({
    transports: [
        new (winston.transports.Console)({ level : 'silly' })


// Only messages with level 'info' or higher will be logged after this.
log.transports.Console.level = 'info';

I guess, it works similarly for file but I haven't tried that.

Discussion courtesy of: weekens

If you want to change the log level on the fly. Like for when you need to trace production issue for short amount of time; then revert to error log level. You can use a dynamic logger provided you can expose a service on the web

Discussion courtesy of: Yann VR

This recipe can be found in it's original form on Stack Over Flow.