Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用Winston进行多文件传输_Node.js_Express_Winston - Fatal编程技术网

Node.js 使用Winston进行多文件传输

Node.js 使用Winston进行多文件传输,node.js,express,winston,Node.js,Express,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 whi

我试图在温斯顿使用多个运输工具。这起作用了……有点。我为应用程序的审核日志设置了一个传输,自定义级别为“审核”,传输级别为“信息”

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

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

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