Winston的字符串插值在Node.js中不起作用

时间:2019-06-12 11:49:47

标签: javascript node.js logging winston

当我尝试使用Winston logger进行登录时,无法在nodejs中正确看到ID和错误。

这是logger.js文件:

const path = require('path');
const winston = require('winston');

module.exports = function (app) {
  global.logger = new Logger(app.config.get('app').logLevel);
};

function Logger(logLevel='info') {
  return winston.createLogger({
    levels: {alert: 0, error: 1, warn: 2, info: 3, verbose: 4, debug: 5, silly: 6},
    level: logLevel,
    transports: [
      new (winston.transports.Console)({level: logLevel}),
      new (require('winston-daily-rotate-file'))({filename: path.resolve(__dirname + '/../../logs/app.log')})
    ]
  });
}

在Customer.js中,我尝试这样登录:

logger.warn('Failed to update patient during user(%s) creation', userInstance.id, err);

这就是我得到的输出:

 {"_bsontype":"ObjectID","id":{"type":"Buffer","data":[93,0,225,203,227,175,68,1,61,50,162,194]},"level":"warn","message":"Failed to update patient during user(%s) creation"}

如果我错了,请纠正我。

1 个答案:

答案 0 :(得分:1)

来自winston's doc

  

log方法使用util.format提供字符串插值。必须使用format.splat()启用它。

似乎您需要在记录器初始化期间指定它。通过同一链接:

const logger = createLogger({
  format: format.combine(
    format.splat(),
    format.simple()
  ),
  transports: [new transports.Console()]
});