簡體   English   中英

winston.js信息方法不傳遞元數據

[英]winston.js info method not passing metadata

當我通過winston傳遞元數據時,快捷方式winston.info方法不會傳遞元數據。

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

我希望兩個電話打印出元數據?

通過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;
};

我認為這很可能是由於溫斯頓v0.7.1的回歸。

鑒於此輸入:

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

您將獲得以下輸出

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

與:

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

在接受PR之前,我已經更新了https://github.com/flatiron/winston/pull/246以反映回歸。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM