NodeJS Winston记录器记录到两个文件

xfyts7mz  于 2022-11-22  发布在  Node.js
关注(0)|答案(2)|浏览(171)

我有两个不同的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');
            }
        })
    ]
});

然后我还有一个要求:

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');
            }
        })
    ]
});

但是,我还有一个verbose.log文件,我希望安全和系统日志文件中的所有日志也都写入到verbose文件中。
我尝试将文件名(即verbose.log)添加到每个传输中,但这不起作用,因为我收到错误消息

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

cld4siwp1#

这是温斯顿的一个常见问题,但很容易解决。
由于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');
            }
        })
    ]
});

名称本身并不重要,只要它是不同的,一个简单的方法是使用文件名沿着一些其他的传输标识符。
来源:issue #101 of the official Github
编辑:

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
//<=
    ]
});
goucqfw6

goucqfw62#

您可以根据需要创建两个或多个日志记录器并添加name属性来过滤日志。在两个日志记录器中添加一个公共传输函数对我很有效。

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

相关问题