我有两个记录器:
var category1 = winston.loggers.get('category1');
var category2 = winston.loggers.get('category2');
我需要获取他们的自定义名称(或默认值)。
CustomLogger.prototype.log = function (level, msg, meta, callback) {
// i need logger name here! (category1 or category2 or undefined / default)
}
如何正确地做到这一点?
感谢
答案 0 :(得分:2)
作为修补程序..
// fix to get logger name as metadata
var originalGetLogger = winston.Container.prototype.get // winston.loggers.get(loggerName);
winston.Container.prototype.get = function(loggerName,options){
var logger = originalGetLogger.call(winston.loggers,loggerName,options)
logger.rewriters.push(function(level, msg, meta) {
meta._l = loggerName;
return meta;
});
return logger;
}
im overriding get fn添加重写器,并使用重写器将logger名称添加到元数据。
// now each log will insert LOGGERNAME to meta
var loggerA = winston.loggers.get('LOGGERNAMEA');
var loggerB = winston.loggers.get('LOGGERNAMEB');
如果有更好的事情让我知道。谢谢。