如何在winston中设置多个独家自定义级别?

时间:2018-01-24 12:18:42

标签: logging winston

我尝试在winston中设置多个独占自定义级别。例如,默认error级别仅在为error级别定义的传输中记录。但是,info的传输将记录infoerror

有这样的选项吗?

这可能是this one的重复问题。

1 个答案:

答案 0 :(得分:0)

您必须定义自定义日志级别并将其传递给您的传输

示例: -

 //your custom levels 
    const debugLevel = {
      levels: {
        error: 0,
        custom: 1,
        custom2: 2,
        custom3: 3,
        custom4: 4,
      },
    };

    //create your custom logger and pass debuglevels to the transport
    const customLogger = createLogger({
      levels: debugLevel.levels,
      format: combine(
        label({ label: 'customLogger' }),
      ),
  transports: [
    new (transports.File)({
      level : 'custom4',
      filename: 'your Logger fileName path',
      json: true,
    }),
  ],

    });

现在最重要的部分,您必须配置传输级别 因此,如果您将其设置为custom4,则表示它将记录custom4 + custom3 + .... +错误,我将其定义为custom4以记录所有自定义级别。

例如: - 如果我们将level设置为custom3

customLogger.custom4('message') ; //this will not be logged into a file 
customLogger.custom3('message') ; //this will be logged 
customLogger.custom2('message') ; //this will be logged 
customLogger.custom1('message') ; //this will be logged 
customLogger.error('message') ; //this will be logged 

如果我们将级别设置为错误

customLogger.custom4('message') ; //this will not  
customLogger.custom3('message') ; //this will not
customLogger.custom2('message') ; //this will not 
customLogger.custom1('message') ; //this will not
customLogger.error('message') ; //only this will be logged