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.
You can change the logging level in runtime by modifying the
level
property of the appropriate transport:I guess, it works similarly for file but I haven't tried that.
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
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:
Above code will set log level to
warn
, which meanssilly
,verbose
andinfo
will not be output to somefile.log, whilewarn
,debug
anderror
will.You can also define your own levels:
Note that it's better to always include the 6 predefined levels in your own custom levels, in case somewhere used the predefined levels.
Looks like there is a level option in the options passed covered here
From that doc:
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:
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.
If you are using the default logger, you can adjust the log levels like this:
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:
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: