Multiple log files with Winston?

后端 未结 5 1458
故里飘歌
故里飘歌 2020-12-14 09:36

We\'d like to use Winston for our logging in Node.js. But, we can\'t figure out how to have two log files: one for just errors, and one for everything else.

Doing th

5条回答
  •  时光说笑
    2020-12-14 09:53

    Unfortunately, the patch that pesho mentioned seems to be still not included in the official version (see stephenbeeson's comment in the pull request #149).

    So, I used a workaround instead. As winston compares the name attributes, you can fool it by defining the name yourself:

    winston = require 'winston'
    
    logger = new winston.Logger
      transports: [
        new winston.transports.File
          name: 'file#debug'
          level: 'debug'
          filename: '/tmp/debug.log'
        new winston.transports.File
          name: 'file#error'
          level: 'error'
          filename: '/tmp/error.log'
      ]
    logger.error 'error' # both logs
    logger.debug 'debug' # on debug log
    

    Maybe not elegant, but at least it works.

提交回复
热议问题