Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 370
Vishnu Avatar asked Mar 21 '26 21:03

Vishnu


1 Answers

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()]
});
like image 117
Jean Pacher Avatar answered Mar 24 '26 14:03

Jean Pacher