nodejs winston – 使用多个参数logging

我试图用winston进行日志logging,但是我看到,如果我想用2个参数获取数据,那么我得到的日志,但如果我想获得3个参数,那么我没有得到的数据,

例如:

logger = new (winston.Logger)({ transports: [ new (winston.transports.Console)({colorize : true, timestamp:true}), ] }); 

现在试图获取日志如下:

  1. logger.info("We got the data %s, %d", data, port); O/P: We got the data %s, %d => No data and port value 2. logger.info("We got the data, port", data, port); O/P: We got the data, port => No data and port value 3. logger.info("We got the data", data); ===> Getting the data correctly. 

你可以让我知道我在1,2失踪了还是winston没有logging数据在情况1和2真的?

你假设有一个可变参数风格的API,但没有。 完整的API是level, msg, meta, callback ,但是当你使用这个级别的方法之一,那么它只是msg, meta, callback 。 你需要构build一个单一的meta对象,并通过它。

https://github.com/flatiron/winston/blob/master/lib/winston/logger.js#L115

 // // ### function log (level, msg, [meta], callback) // #### @level {string} Level at which to log the message. // #### @msg {string} Message to log // #### @meta {Object} **Optional** Additional metadata to attach // #### @callback {function} Continuation to respond to when complete. // Core logging method exposed to Winston. Metadata is optional. // 

https://github.com/flatiron/winston#logging-with-metadata

我通过包装winston函数,并使用包装来解决这个问题。 我希望现在有更好的解决scheme。

 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)]); }; 

///然后像使用logging器一样返回包装。 ///并使用像log.info(1,2,3,4,“foo”,“bar”);