如何获取Winston日报文件旋​​转以记录到相应级别的文件 [英] How to get Winston daily file rotate to log to corresponding level file

查看:91
本文介绍了如何获取Winston日报文件旋​​转以记录到相应级别的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为我的应用程序定义了自定义级别.它们如下.

protected levels: Level = {
        "error": 0,
        "warn": 1,
        "info": 2,
        "debug": 3,
        "trace": 4
    };

我正在使用每日文件轮换传输以获取单独文件中的每日日志.

    const options: Object = {
        name: this.level,
        filename: logFilePath,
        dirname: WinstonLogAgent.DIR_LOG,
        datePattern: "yyyyMMdd.",
        prepend: true,
        level: this.level,
        levels: this.levels,
        maxsize: this.maxFileSize,
        maxFiles: this.maxFileCount,
        handleExceptions: true,
        humanReadableUnhandledException: true
    };

    this.transportInstance.push(new (winston.transports.DailyRotateFile)(options));

如果我将日志级别定义为"info",它将创建一个名为info.log的日志文件,并将记录级别"info","warn"和"error"(跟踪和调试将被忽略). /p>

但是我想要的行为是不同的.如果我将级别指定为'info'并且我正在记录级别'info','warn'和'error',则应该为每种类型的日志创建单独的文件.即信息"级别应记录到info.log,警告"级别应记录到warn.log.

我尝试指定五种不同的每日文件轮换传输方式,每一种都有唯一的级别.然后我发现的问题是有重复的日志条目. 例如,如果记录错误"级别,它将记录到info.log,warn.log&当日志记录级别设置为info时,会显示error.log.

我如何实现我的目标?

解决方案

根据Winston的文档,默认行为是记录所有至少具有指定重要性(又称为日志记录级别)的消息.

Winston允许您在每个传输中定义 level 属性,该属性指定传输应记录的消息的 maximum 最高级别.

但是有多种方法可以满足您的要求.
我将尝试向您展示一些可能性,您可以选择最适合自己的方法.


1.自定义传输方式(推荐):

您可以创建自定义传输并仅记录所需的级别.
这是一个示例,仅供您参考:

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.使用 winston-levelonly

这是原始Winston软件包的叉子.叉子位于 https://github.com/damianof/winston
此版本添加了 levelOnly 选项,以使winston仅记录指定级别.


最后,我想鼓励您阅读以下相关讨论:

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

protected levels: Level = {
        "error": 0,
        "warn": 1,
        "info": 2,
        "debug": 3,
        "trace": 4
    };

I am using daily file rotate transport to get daily log in separate files.

    const options: Object = {
        name: this.level,
        filename: logFilePath,
        dirname: WinstonLogAgent.DIR_LOG,
        datePattern: "yyyyMMdd.",
        prepend: true,
        level: this.level,
        levels: this.levels,
        maxsize: this.maxFileSize,
        maxFiles: this.maxFileCount,
        handleExceptions: true,
        humanReadableUnhandledException: true
    };

    this.transportInstance.push(new (winston.transports.DailyRotateFile)(options));

If i define log level to be 'info', it will create one log file named info.log and will log for levels 'info' , 'warn' and 'error' (trace and debug will be ignored).

But behaviour i wanted was different. If i am specifying level to be 'info' and i am logging levels 'info' , 'warn' and 'error' , then there should be separate files created for each type of log . i.e 'info' level should be logged to info.log and 'warn' level to be logged to warn.log.

I have tried specifying five different daily file rotate transport ,each with unique level. Then the problem i find is that there is duplicate log entries. For example , if am logging 'error' level , it would log to info.log , warn.log & error.log when logging level is set to info.

How can i achieve my objective?

解决方案

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:

这篇关于如何获取Winston日报文件旋​​转以记录到相应级别的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆