【问题标题】:How use a "commander" CLI flag to set the the "winston" logging level in a node library如何使用“commander”CLI 标志在节点库中设置“winston”日志记录级别
【发布时间】:2022-01-29 04:46:43
【问题描述】:

鉴于此示例指挥官应用程序:

// index.ts

// Config Winston:
winston.configure({
    level: 'info',
    format: winston.format.combine(winston.format.splat(), winston.format.cli()),
    transports: [new winston.transports.Console({})],
});

winston.info('Started CLI')

// Configure commander
const cli = new Command()
    .option('--debug', 'Debug mode', false) // Or --verbose, it doesn't really matter.
    .action(actionCallback); // Imported.

cli.parse();

根据提供的指挥官选项--debug,我如何设置winston日志记录级别?

我可以使用 DEBUG 环境变量,但这有点破坏了 CLI 中 --debug 标志的用途。有什么建议吗?

【问题讨论】:

    标签: node.js logging command-line-interface winston node-commander


    【解决方案1】:

    最简单的解决方案,实际上是解决方法,是自己检查--debug 标志:

    const debugLevel = process.argv.indexOf('--debug') != -1 ? 'debug' : 'info';
    
    winston.configure({
        level: debugLevel,
        format: winston.format.combine(winston.format.splat(), winston.format.cli()),
        transports: [new winston.transports.Console({})],
    });
    

    【讨论】:

      猜你喜欢
      • 2014-01-22
      • 2013-03-10
      • 2016-11-27
      • 2023-03-23
      • 1970-01-01
      • 2023-03-24
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多