How to set log level in Winston/Node.js

后端 未结 1 640
[愿得一人]
[愿得一人] 2021-02-12 02:56

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

1条回答
  •  忘了有多久
    2021-02-12 03:23

    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');
    

    0 讨论(0)
提交回复
热议问题