bithavoc / express-winston

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

Feature: Logging route params #144

Closed charlesdeuter closed 7 years ago

charlesdeuter commented 7 years ago

Is it possible to pull the route params, as well as the raw route path and add it in with the meta object?

This would be a helpful feature.

rosston commented 7 years ago

Is that info on req? If so, you should just be able to add it to the requestWhitelist and it'll automatically go on the req in the meta object.

ruiming commented 7 years ago

+1

params in requestWhitelist can not capture params and it always return empty object.

rosston commented 7 years ago

Here's the test I just did for logging req.params:

var requestLogger = new expressWinston.logger(({
  meta: true,
  transports: [
      new winston.transports.File({filename: 'winston.log'})
  ]
}));

app.all('*', requestLogger);

app.use('/foo/:code', (req, res, next) => {
  res.setHeader('Content-Type', 'application/json');
  res.status(400).send({
      message: {baz: 'qux'}
  });
});

When requesting /foo/bar?norf, here's the (trimmed) JSON I get in winston.log:

{
  ...
  "req": {
    ...
    "httpVersion": "1.1",
    "method": "GET",
    "originalUrl": "/foo/bar?norf",
    "params": {
      "code": "bar"
    },
    "query": {
      "norf": ""
    },
    "url": "/foo/bar?norf"
  },
  ...
}

I think that covers what you said @ruiming.

ruiming commented 7 years ago

@rosston I test again and you are right. But the param will not be captured when I write the code like this.

    app.use(require('app/middleware/access-log'))
    app.use('/test/:id', function (req, res, next) {
      return next()
    })

the output of param is an empty object. I write this just to simply test the middleware I wrote. When I change the code from return next() to res.json() or res.status().send() and the param will show. Anyway, thank you.

rosston commented 7 years ago

@ruiming Yeah, express-winston only logs at time of res.end(), which will get skipped entirely in the case of return next().

rosston commented 7 years ago

@charlesdeuter Do you have any extra info to add to your original request? I tried to cover it as much as possible in https://github.com/bithavoc/express-winston/issues/144#issuecomment-283784975, but I'll need more detail to take it any further.

charlesdeuter commented 7 years ago

ahhhh, I see you have to add it to the requestWhiteList. Thanks for the hint!

rosston commented 7 years ago

Closing this since I think we've got it all figured out. @charlesdeuter, let me know if you need anything else.