0

我正在尝试在 Winston 中使用多种传输方式。这是工作......排序。我为应用程序的审核日志设置了传输,自定义级别为“审核”,传输为“信息”。

var winston_mongo_options = {
    level: 'audit', // level that should be logged
    safe: true, //makes sure writes happen before firing log event
    db: config.db.db,   // db in which to write logs
    host: config.db.host,
    port: config.db.port,
    collection: config.db.audit_collection  // collection we want logging to occur
};
if (config.db.user) {
  winston_mongo_options.username = config.db.user;
  winston_mongo_options.password = config.db.pass;
}

var custom_levels = winston.config.syslog.levels;
custom_levels.audit = 8;
var logger = new (winston.Logger)({
    levels: custom_levels,
    transports : [
        new (winston.transports.MongoDB)(winston_mongo_options),
        new (winston.transports.File)({
            level: 'info',
            silent: false,
            colorize: true,
            timestamp: true,
            filename: config.logs.debug,
            maxsize: 500000,
            maxFiles: 5,
            json: true
        })
    ],
    exceptionHandlers: [ 
        new (winston.transports.File)({
            silent: false,
            colorize: false,
            timestamp: true,
            filename: config.logs.exception,
            maxsize: 500000,
            maxFiles: 5,
            json: true
        })
    ]
});

module.exports.logger = logger;

显然,我需要这个文件,我想在哪里/什么时候做任何日志记录。当想要将某些信息单独发送到日志时,就会出现问题。

logger.audit('Server Started - to DB');
logger.info('Server Started - to info');

这两行应该写入单独的日志。第一行被正确写入数据库和信息日志文件。我究竟做错了什么?

4

1 回答 1

2

已解决:问题是我如何定义级别。我没有意识到 Winston 日志记录的工作方式是日志将接收所有 >= 定义的级别。所以我的 'info' 传输是 0 级,正在接收发送到 8 级的 'audit' 传输的消息。我将 'audit' 设置为 0 级,它停止显示在 'info' 日志中。然后我通过为审计数据库日志创建一个全新的记录器找到了一个更好的解决方案。

于 2014-09-18T14:40:47.123 回答