简体   繁体   English

温斯顿记录器记录到两个文件

[英]Winston logger to log to two files

I have two different winston loggers which look like this (there are two separate ones as they write different logs depending on my functions) -:我有两个不同的 winston 记录器,它们看起来像这样(有两个独立的记录器,因为它们根据我的功能编写不同的日志)-:

var security = new(winston.Logger)({
    transports: [
        new(require('winston-daily-rotate-file'))({
            filename: logDir + '/-security.log',
            datePattern: 'dd-MM-yyyy',
            prepend: true,
            json: false,
            timestamp: function() {
                return moment().format('D/MM/YYYY HH:mm:ss:SSS');
            }
        })
    ]
});

And then I have one more for:然后我还有一个:

var system = new(winston.Logger)({
    transports: [
        new(require('winston-daily-rotate-file'))({
            filename: logDir + '/-security.log',
            datePattern: 'dd-MM-yyyy',
            prepend: true,
            json: false,
            timestamp: function() {
                return moment().format('D/MM/YYYY HH:mm:ss:SSS');
            }
        })
    ]
});

However, I also have a verbose.log file, where I want all of the logs from the security and system log file to be written to the verbose too.但是,我还有一个verbose.log文件,我希望将安全和系统日志文件中的所有日志也写入到 verbose 文件中。 Whats the best way of doing that?最好的方法是什么?

I tried adding the filename (ie verbose.log) to each of the transports, but this does not work as I get the error我尝试将文件名(即 verbose.log)添加到每个传输,但这不起作用,因为我收到错误

Error: Transport already attached: dailyRotateFile, assign a different name

It is a common problem with winston, but a simple one to solve. 这是winston的常见问题,但需要解决一个简单的问题。
Due to how winston is coded, it cannot differenciate your two transport himself, and need you to explicitly declare them as separate by giving them different names: 由于winston的编码方式,它不能自己区分你的两个传输,并且需要你通过给它们不同的名称明确地声明它们是独立的:

var security = new(winston.Logger)({
    transports: [
        new(require('winston-daily-rotate-file'))({
//=>
            name: 'foo',
//<=
            filename: logDir + '/-security.log',
            datePattern: 'dd-MM-yyyy',
            prepend: true,
            json: false,
            timestamp: function() {
                return moment().format('D/MM/YYYY HH:mm:ss:SSS');
            }
        })
    ]
});

var system = new(winston.Logger)({
    transports: [
        new(require('winston-daily-rotate-file'))({
//=>
            name: 'bar',
//<=
            filename: logDir + '/-security.log',
            datePattern: 'dd-MM-yyyy',
            prepend: true,
            json: false,
            timestamp: function() {
                return moment().format('D/MM/YYYY HH:mm:ss:SSS');
            }
        })
    ]
});

The name itself doesn't matter as long as it is different, a easy way to do that is to use the filename along with some other identifier for the transport. 名称本身并不重要,只要它不同,一个简单的方法是使用文件名以及传输的其他标识符。

Source: issue #101 of the official Github 资料来源: 官方Github第101期

EDIT: 编辑:

var verbose = new(require('winston-daily-rotate-file'))({
    name: 'baz',
    filename: logDir + '/-verbose.log',
    datePattern: 'dd-MM-yyyy',
    prepend: true,
    json: false,
    timestamp: function() {
        return moment().format('D/MM/YYYY HH:mm:ss:SSS');
    }
})

var security = new(winston.Logger)({
    transports: [
        new(require('winston-daily-rotate-file'))({
//=>
            name: 'foo',
//<=
            filename: logDir + '/-security.log',
            datePattern: 'dd-MM-yyyy',
            prepend: true,
            json: false,
            timestamp: function() {
                return moment().format('D/MM/YYYY HH:mm:ss:SSS');
            }
        }),
//=>
        verbose
//<=
    ]
});

var system = new(winston.Logger)({
    transports: [
        new(require('winston-daily-rotate-file'))({
//=>
            name: 'bar',
//<=
            filename: logDir + '/-security.log',
            datePattern: 'dd-MM-yyyy',
            prepend: true,
            json: false,
            timestamp: function() {
                return moment().format('D/MM/YYYY HH:mm:ss:SSS');
            }
        }),
//=>
        verbose
//<=
    ]
});

You can filter out logs by creating two or more loggers as per your requirement and adding name property to them.您可以通过根据您的要求创建两个或多个记录器并向它们添加名称属性来过滤掉日志。 Adding a common transport function in both logger worked for me.在两个记录器中添加一个公共传输 function 对我有用。

const winston = require('winston');

const combinedLogger = new winston.transports.File({
  name: "data",
  filename: "./logs/combined.log",
  level: "info",
})

const infoLogger = winston.createLogger({
  defaultMeta: { service: "log-service" },
  transports: [
    new winston.transports.File({
      name: "info",
      filename: "./logs/info.log",
      level: "info",
    }),
    combinedLogger
  ],
});

const errorLogger = winston.createLogger({
  defaultMeta: { service: "log-service" },
  transports: [
    new winston.transports.File({
      name: "error",
      filename: "./logs/error.log",
      level: "error",
    }),
    combinedLogger
  ],
});

const logger = {
  info: (params) => {
    return infoLogger.info(params);
  },
  error: (params) => {
    return errorLogger.error(params);
  },
};

logger.info("info log");    // Will goto info.log & combined.log
logger.error("error log");  // Will goto error.log & combined.log

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM