Disable winston logging when running unit tests?

前端 未结 7 1061
暗喜
暗喜 2021-02-05 03:18

Can Winston logging be selectively disabled when executing unit tests of a node module?

Ideally, I\'d like to have logging for informational and debug purposes when the

7条回答
  •  臣服心动
    2021-02-05 03:50

    Winston transports have a silent property that you can set, which is probably a little nicer than removing the entire transport.

    I add a name to the transports to make is a little easier like this:

    var logger = new winston.Logger();
    
    logger.add(winston.transports.Console, {
        name: 'console.info',
        colorize: true,
        showLevel: true,
        formatter: consoleFormatter,
    })
    

    Then in the test or set-up I can selectively turn logging on and off with:

    logger.transports['console.info'].silent = true  // turns off
    logger.transports['console.info'].silent = false // logging back on
    

提交回复
热议问题