简体   繁体   中英

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.

Here is the logger.js file:

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:

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. It must be enabled using 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()]
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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