How to set log level in Winston/Node.js
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 loggerwinston.log('debug', 'default logger being used');// custom loggermyLogger.log('debug', 'custom logger being used');
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.
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.