31

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.

6 Answers 6

37

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');
5
  • 2
    yep, this is the correct answer, the other ones require creating a new logger and don't adjust the settings of the existing default loggers created by winston.
    – user9903
    Aug 1, 2014 at 3:06
  • 1
    This was the single hardest bit of info to find! Thanks @AndreasPizsa! Mar 16, 2018 at 13:44
  • 2
    @RudolfOlah The correct answer is what the docs recommend, which is creating a new logger. Changing the log level of the default logger (or of any other already created logging instance) dynamically has been discussed in winston's github issues countless times and the outcome is in most cases - while it might work with most or even recent/current versions - to not do it. Don't just call an answer "correct" just because it matches your personal preference better. However there are libraries wrapping around winston that provide this functionality.
    – Num Lock
    Feb 14, 2020 at 7:50
  • 1
    Appreciated, thanks. Mind sharing a few links or search terms to such libraries? @RudolfOlah Feb 14, 2020 at 10:27
  • Oh, that’s funny - accidentally mentioned RudolfOlah when in fact I meant @Num Lock. Thank you both for your contributions and insight! Feb 16, 2020 at 15:17
25

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.

6
  • 1
    bryanmac, thanks for the reply but I am afreaid it still doesnt answer my question. Let me clarify. At my previous job, we were using a custom logging module which was very similar to winston featurewise. The module relied on an entry in a config file which set the log level. If we had to debug any issue, we would change the entry in the config file so as to increase the verbosity of the log. But during other times, we would set the level to its lowest level so as to log only errors. Is something like that possible with winston? Mar 21, 2013 at 2:33
  • 1
    The programmatic code above could read from a log file to set the initial logging level. Now, to change at runtime from the intial/default level, you could expose an API that turns around and calls the setLevel method I mentioned. That way you could have an admin page that reads the log level and a way to set it.
    – bryanmac
    Mar 21, 2013 at 4:42
  • Bryan, I am going to accept your initial answer + your comment as the answer to my question. Thanks. Mar 21, 2013 at 18:10
  • Thanks - if you have any other feedback or thoughts on how you used it or it worked out, that's appreciated. I was in the process of going through the same thing right now :)
    – bryanmac
    Mar 21, 2013 at 23:00
  • Bryan, I will certainly comeback and share my implementation details here. Mar 22, 2013 at 18:30
8

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.

5
  • Chen, Thanks for your answer. I have accepted Bryan's answer above. But yours was quite helpful too. Mar 21, 2013 at 18:11
  • 3
    I'm sorry but @Chen is wrong. Debug is lower than verbose. Suggested an edit. Aug 12, 2014 at 16:33
  • @filsmick You are right. Debug was higher than verbose when I wrote this answer, but now verbose is higher.
    – Chen
    Aug 14, 2014 at 3:38
  • @Chen Oh, didn't know it once was higher. I tilted when I saw that, because it didn't make sense, so I tested it :) Aug 14, 2014 at 9:33
  • Is it possible for a level to hear its own level only ? Not of any level having value greater than it ? But only and only of its own level ?
    – Gaurav
    May 23, 2015 at 17:45
3

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.

0

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 https://github.com/yannvr/Winston-dynamic-loglevel

0

apart from this you can cleanly achieve this by imlplementing runtime-node-refresh follow this link for more.

1
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – L8R
    Oct 27, 2022 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.