Disable winston logging when running unit tests?

前端 未结 7 1064
暗喜
暗喜 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:51

    The set up stuff did not work for me, I am using winston v3.1.0, there is a new way to create loggers.

    From the winston site: https://github.com/winstonjs/winston

    The recommended way to use winston is to create your own logger. The simplest way to do this is using winston.createLogger:

    const logger = winston.createLogger({
        level: 'info',
        format: winston.format.json(),
        transports: [
          //
          // - Write to all logs with level `info` and below to `combined.log` 
          // - Write all logs error (and below) to `error.log`.
          //
          new winston.transports.File({ filename: 'error.log', level: 'error' }),
          new winston.transports.File({ filename: 'combined.log' })
        ]
      });
    
      //
      // If we're not in production then log to the `console` with the format:
      // `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
      // 
      if (process.env.NODE_ENV !== 'production') {
        logger.add(new winston.transports.Console({
        format: winston.format.simple()
      }));
    }
    

    So I do this in my logger.js

    if (process.env.NODE_ENV === 'test') {
        return winston.createLogger({
          transports: [ new winston.transports.Console({ level: 'error'}) ]
        });
    }
    

    This stops all log messages unless you have an error, which I would like to see to help with debugging any issues.

    Hope this helps.

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