Winston如何将日志发送到不同的传输?

我在系统中发生了不同的事件,并希望将其中的一些logging到一个文件中,另一些logging到另一个文件中。

例如:

  • 像'start','stop'这样的服务器事件会转到'serverLogger'传输的'server.log'。
  • 用户事件'login','注销','注册'将被放在与UsersLogger'users.log'
  • 付款事件,如“已付款”,“已拒绝”将在PaymentsLogger的“payments.log”中。

在系统中,我会运行它,如:

logger.log(ServerLogger, 'start'); logger.log(UsersLogger, 'login','john'); logger.log(PaymentsLogger, 'paid','100', 'john'); 

我如何使它像这样工作,以便当我想login到某个特定的logging器,它会被使用?

我是否应该将每个logging器注册为这样的新的winston实例?

 const serverLogger = new winston.Logger() const usersLogger = new winston.Logger() const paymentsLogger = new winston.Logger() 

虽然这可能不是一个完美的解决scheme,但我发现自己处于类似的情况。 我有不同的文件,可能会或可能不会被激活,pipe理应用程序的不同部分。

我的解决办法是根据winston制作我自己的“logging器”。 由于每个源代码文件都需要一个login到不同文件的logging器和一个“常见”文件,因此我创build了一个“生成器”,而不是直接要求winston:

log.js:

 'use strict'; const util = require('util'), winston = require('winston'), config = require('./config.json'); //If I don't want to log in files and only in console let testMode = (config.log.mode === 'test'); //"Common" log file const fileTransport = new (winston.transports.File)({ timestamp: true, name: config.log.all.file, filename: config.log.all.file, level: config.log.all.level }), //"Common" error log file errorTransport = new (winston.transports.File)({ timestamp: true, name: config.log.error.file, filename: config.log.error.file, level: 'error' }); //Logs are also sent in mongoDB, with the same schema as in the files let mongoTransport = {}, mongoErrorTransport = {}; if(!testMode) { //Add winston.transport.MongoDB require('winston-mongodb'); mongoTransport = new (winston.transports.MongoDB)({ name: 'all', host: config.log.db.host, safe: config.log.db.safe, collection: 'all', level: config.log.all.level, db: config.log.db.db }); mongoErrorTransport = new (winston.transports.MongoDB)({ name: 'error', host: config.log.db.host, safe: config.log.db.safe, collection: 'error', level: 'error', db: config.log.db.db }); } function getTransports(file) { let transports = []; //Log in the console transports.push(new (winston.transports.Console)({ timestamp: true, level: config.log.all.level, formatter: (args) => { let d = new Date(); return d.getFullYear() + '/' + d.getMonth(), + '/' + d.getDate(), + ' ' + d.getHours(), + ':' + d.getMinutes(), + ':' + d.getSeconds(), + ':' + d.getMilliseconds(), + ' - ' + file + ' - ' + args.level + ' -\t' + args.message + '\t' + util.inspect(args.meta); } })); if(testMode) { return transports; } let name, level, filename; transports.push(fileTransport); transports.push(errorTransport); transports.push(mongoTransport); transports.push(mongoErrorTransport); //Module specific logs if(config.log[file] && config.log[file].file) { name = config.log[file].file; } else { name = file; } if(config.log[file] && config.log[file].level) { level = config.log[file].level; } else if(config.log.default && config.log.default.level) { level = config.log.default.level; } else { level = 'info'; } if(config.log[file] && config.log[file].file) { filename = config.log[file].file; } else if(config.log.default && config.log.default.file) { filename = config.log.default.path + file + '.log'; } else if(config.log.default && config.log.default.path) { filename = config.log.default.file; } else { filename = './log/' + file + '.log'; } //Module specific log file transports.push(new (winston.transports.File)( { timestamp: true, name: name, level: level, filename: filename } )); //Module specific Mongo collection for logs transports.push(new (winston.transports.MongoDB)({ name: 'mongo' + file, host: config.log.db.host, safe: config.log.db.safe, collection: file, level: level, db: config.log.db.db })); return transports; } //Generator module.exports = (file) => { let transports = getTransports(file); return new (winston.Logger)({ rewriters: [ (level, msg, meta) => { meta.app = file + '.js'; return meta; } ], transports: transports }); }; 

被称为像:

 'use strict'; const Logger = require('./log.js'), logger = Logger('myModule'); logger.debug('Hi'); logger.error('Oops'); 

虽然这是一个完美的解决scheme,可能不适用于您的具体问题,但类似的东西可能比手动创build每个logging器更清洁。