How to get Winston daily file rotate to log to corresponding level file

前端 未结 1 1693
故里飘歌
故里飘歌 2021-01-23 12:50

I have defined custom levels for my application .They are as follows.

protected levels: Level = {
        \"error\": 0,
        \"warn\": 1,
        \"info\": 2,         


        
相关标签:
1条回答
  • 2021-01-23 13:47

    According to Winston's documentation, the default behavior is to log all the messages which have at least the specifies importance aka logging level.

    Winston allows you to define a level property on each transport which specifies the maximum level of messages that a transport should log.

    But there are ways to achieve your requirements.
    I'll try to show you some of the possibilities, you can choose the method that works the best for you.


    1. Custom Transports (Recommended):

    You can create a custom transport and log only the levels you want.
    Here is an example just to give you an idea:

    let mainLogger = new (winston.Logger)({
      transports: [
        new (winston.transports.Console)(),
      ]
    });
    
    class CustomTransport extends winston.Transport {
      constructor(options) {
        super(options);
        this.name = 'customLogger';
        this.level = options && options.level || 'info';
        this.levelOnly = options && options.levelOnly;
        this.levels = options && options.levels || [];
      }
    
      log(level, msg, meta, callback) {
        if (!this.levelOnly || this.levels.indexOf(level) > -1) {
          mainLogger[level](msg, meta);
        }
        callback(null, true);
      }
    }
    
    winston.transports.CustomTransport = CustomTransport;
    
    let myLogger = new winston.Logger({
      transports: [
        new (winston.transports.CustomTransport)({
          levelOnly: true,
          levels: ['info'],
        }),
      ]
    });
    
    myLogger.info('will be logged');
    myLogger.warn('will NOT be logged');
    myLogger.info('will be logged as well');
    

    2. Use winston-levelonly

    This is a fork of the original winston package. The fork is at https://github.com/damianof/winston
    This version adds a levelOnly option to make winston log only the specified level.


    In the end, I would like to encourage you to read these relevant discussions:

    • https://github.com/winstonjs/winston/issues/614
    • https://github.com/winstonjs/winston/issues/812
    • https://github.com/winstonjs/winston/pull/628
    • Winston Logging - separate levels to separate Transports
    0 讨论(0)
提交回复
热议问题