How to set log level in Winston/Node.js

后端 未结 1 644
[愿得一人]
[愿得一人] 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:29

    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.

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