bithavoc / express-winston

express.js middleware for winstonjs
https://www.npmjs.com/package/express-winston
MIT License
796 stars 187 forks source link

Add custom fields #221

Closed Enado95 closed 4 years ago

Enado95 commented 4 years ago

Is there an option to add customer field?

Like for example I want to add a username field to see made the request to me api.

I already have in the msg like this:

  msg: (req, res) => 'Test' + (res.statusCode == 401 ? ' ' : ` Username: ${req.user.perferred_username}`) 

But its quite challenging to search.

dezoito commented 4 years ago

Have you tried the dynamicMeta: function(req, res) { return [Object]; } option?

I am using it to write the unique request Id to the logs:

app.use(expressWinston.logger({
  transports: [
    new winston.transports.File({
     ....
    }),
  ],
  format: winston.format.combine(
    winston.format.timestamp(),
    winston.format.json(),
  ),
  dynamicMeta: function(req, res) {
    return { reqId: req.id };
  }
}));
Enado95 commented 4 years ago

Thanks I'll check it out

Enado95 commented 4 years ago

This works. Thanks for the assistance @dezoito