简体   繁体   English

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

[英]String interpolation with winston not working in Nodejs

When I tried to log using winston logger, I am not able to see the id and error properly in nodejs. 当我尝试使用Winston logger记录日志时,无法在nodejs中正确看到ID和错误。

Here is the logger.js file: 这是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')})
    ]
  });
}

In Customer.js, I tried to log like this: 在Customer.js中,我尝试这样记录:

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

This is what I am getting as output: 这是我得到的输出:

 {"_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"}

Please correct me if I am wrong. 如果我错了,请纠正我。

From winston's doc : 温斯顿的文件

The log method provides the string interpolation using util.format. log方法使用util.format提供字符串插值。 It must be enabled using format.splat(). 必须使用format.splat()启用它。

It seems you need to specify it during the logger initialization. 看来您需要在记录器初始化期间指定它。 From the same link: 从同一链接:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM