Disable winston logging when running unit tests?

前端 未结 7 1065
暗喜
暗喜 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:47

    Here's my setup:

    const { createLogger, format, transports, config } = require("winston");
    
    let level, silent;
    switch (process.env.NODE_ENV) {
      case "production":
        level = "warning";
        silent = false;
        break;
      case "test":
        level = "emerg";
        silent = true;
        break;
      default:
        level = "debug";
        silent = false;
        break;
    }
    
    const options = {
      console: {
        level,
        silent,
        handleExceptions: true,
        format: format.combine(
          format.colorize(),
          format.splat(),
          format.printf(
            info => `${new Date().toISOString()} ${info.level}: ${info.message}`,
          ),
        ),
      },
    };
    
    const logger = createLogger({
      levels: config.syslog.levels,
      transports: [new transports.Console(options.console)],
      exitOnError: false,
    });
    
    module.exports = logger;
    

提交回复
热议问题