本文介绍了Winston日志记录-单独的级别以单独的传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Winston通过2种不同的传输方式进行日志记录-File和MongoDB.我已将文件级别设置为"INFO",将MongoDB级别设置为"ERROR".如果我现在登录,

I am using Winston for logging with 2 different transports - File and MongoDB. I have set the level for File as "INFO" and for MongoDB as "ERROR". If I now log,

log.info('some info...');
log.warn('some Warning...');
log.error('some error...');

所有这些都将进入LogFile,只有Error会进入DB.我只希望信息"消息转到文件",而没有其他消息.

All of these would go to the LogFile, and only Error would go to DB. I want only Info messages to go to File, and none other.

我了解Winston中的系统日志级别,并且只有Error归因于MongoDB,因为它是最高级别.由于INFO级别较低,因此任何INFO级别或更高级别的日志都将进入该文件(根据我的记录器定义)

I understand the system log levels in Winston, and that only Error goes to MongoDB because its the highest level. Since, INFO is a lower level, any log with the level INFO or higher goes to the file (as per my logger definiton)

我已在此处阅读,但找不到答案.即使创建自定义级别,如何将每种传输限制为仅一个日志记录级别?

I have read here but couldn't find an answer. Even if I create custom levels, how can I possibly restrict each transport to only one logging level?

推荐答案

我已经在:

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

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

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

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.

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

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


2. Use winston-levelonly


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


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

这篇关于Winston日志记录-单独的级别以单独的传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 18:15