当我通过 winston 传递元数据时,快捷方式 winston.info 方法不会传递元数据。
var winston = require('winston');
winston.info("winston with metadata", {cluster: "bar"});
winston.log("info", "winston with metadata", {cluster: "baz"});
输出:
info: winston with metadata
info: winston with metadata cluster=baz
我希望这两个调用都能打印出元数据?
挖掘winston代码,它看起来像是为每个级别动态生成快捷函数('info'、'debug'、'error'),并尝试处理其他元数据参数:
//
// ### function setLevels (target, past, current)
// #### @target {Object} Object on which to set levels.
// #### @past {Object} Previous levels set on target.
// #### @current {Object} Current levels to set on target.
// Create functions on the target objects for each level
// in current.levels. If past is defined, remove functions
// for each of those levels.
//
exports.setLevels = function (target, past, current, isDefault) {
if (past) {
Object.keys(past).forEach(function (level) {
delete target[level];
});
}
target.levels = current || config.npm.levels;
if (target.padLevels) {
target.levelLength = exports.longestElement(Object.keys(target.levels));
}
//
// Define prototype methods for each log level
// e.g. target.log('info', msg) <=> target.info(msg)
//
Object.keys(target.levels).forEach(function (level) {
target[level] = function (msg) {
var args = Array.prototype.slice.call(arguments, 1),
callback = args.pop(),
ltype = typeof callback,
meta = args.length ? args : null;
if (ltype !== 'function') {
if (meta && ltype !== 'undefined') {
meta.push(callback);
}
callback = null;
}
if (meta) {
meta = (meta.length <= 1 && meta.shift()) || meta;
return callback
? target.log(level, msg, meta, callback)
: target.log(level, msg, meta)
}
return callback
? target.log(level, msg, callback)
: target.log(level, msg)
};
});
return target;
};