Winston.js information method does not pass metadata

When I pass metadata through winston, the shortston winston.info method does not go through the metadata.

var winston = require('winston'); winston.info("winston with metadata", {cluster: "bar"}); winston.log("info", "winston with metadata", {cluster: "baz"}); 

OUTPUT:

 info: winston with metadata info: winston with metadata cluster=baz 

I expect both calls to print metadata?

Digging through winston code, it looks like it dynamically generates quick access functions for each level ("info", "debug", "error") and tries to process additional metadata arguments:

 // // ### 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 // eg 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; }; 
+4
source share
1 answer

I think this is most likely due to regression in v0.7.1 from winston.

Given this input:

 var winston = require("winston"); var logger = new winston.Logger({ levels: { debug: 0, info: 1, warn: 2, error: 3 }, transports: [ new winston.transports.Console({ level: 'debug', colorize: true }) ] }); console.log('Winston Version: ' + winston.version); console.log('\nProxy Methods\n'); logger.info("Hello world"); logger.warn("Hello world", { winston: { version: winston.version }}); console.log('\nDirect Access Methods\n') logger.log('info', "Hello world"); logger.log('warn', "Hello world", { version: winston.version }); 

You get the following output

 Winston Version: 0.6.2 Proxy Methods info: Hello world warn: Hello world version=0.6.2 Direct Access Methods info: Hello world warn: Hello world version=0.6.2 

Versus:

 Winston Version: 0.7.1 Proxy Methods info: Hello world warn: Hello world Direct Access Methods info: Hello world warn: Hello world version=0.7.1 

I updated https://github.com/flatiron/winston/pull/246 to reflect the regression before accepting the PR.

+3
source

Source: https://habr.com/ru/post/1478886/


All Articles