我通过包装 winston 函数并使用包装器来解决这个问题。我希望现在有更好的解决方案。
var logger = new (winston.Logger)({
transports:[new (winston.transports.Console)({ json : false, timestamp : true, level : 0, colorize : true}),
new (winston.transports.File)({ filename: filepath, json : false, timestamp : true, level : 0, colorize: true })]
});
// pass in function arguments object and returns string with whitespaces
function argumentsToString(v){
// convert arguments object to real array
var args = Array.prototype.slice.call(v);
for(var k in args){
if (typeof args[k] === "object"){
// args[k] = JSON.stringify(args[k]);
args[k] = util.inspect(args[k], false, null, true);
}
}
var str = args.join(" ");
return str;
}
// wrapping the winston function to allow for multiple arguments
var wrap = {};
wrap.info = function () {
logger.log.apply(logger, ["info", argumentsToString(arguments)]);
};
wrap.error = function () {
logger.log.apply(logger, ["error", argumentsToString(arguments)]);
};
wrap.warn = function () {
logger.log.apply(logger, ["warn", argumentsToString(arguments)]);
};
wrap.debug = function () {
logger.log.apply(logger, ["debug", argumentsToString(arguments)]);
};
/// 然后按原样返回 wrap 是您的记录器。/// 并使用类似 log.info(1,2,3,4,"foo","bar");