-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
this.log should be replaced with req.log #74
Comments
Can you add an example to reproduce the problem you are facing? |
I have something like this: router.use(pinoHttp(), (req, res, next) => {
req.log = req.log.child({ something: true })
next();
});
router.get("/example", (req, res) => {
req.log.info("some message")
res.json({ ok: true });
});
I hope this make it more clear. |
if you're always binding the same value, this isn't a good pattern, instead do const logger = require('pino')().child({ something: true })
const pinoHttp = require('pino-http')({ logger })
router.use(pinoHttp) |
for injecting values on a per request basis use the serializers |
Value changes per request. I tried setting that value on |
I think it would be helpful to have a full example of what you are trying to do. |
#74 (comment) app.use(pinoHttp());
const originalRouteMiddleware = (originalRoute) => (req, res, next) => {
req.log = req.log.child({ originalRoute });
next();
}); app.use(pinoHttp({
serializers: {
req(req) {
req.originalRoute = req.raw.originalRoute;
return req;
},
},
}));
const originalRouteMiddleware = (originalRoute) => (req, res, next) => {
req.originalRoute = originalRoute;
next();
}); Route: router.get("/user/:userId", originalRouteMiddleware("/user/:userId"), (req, res) => {
req.log.info("some message");
res.json({ ok: true });
}); So the issues I'm facing:
|
May I get a full example that I can run? Ideally without express (or maybe this is express-only). |
Here it is: https://github.com/GuskiS/pino-express-failure |
Child serializes the request object once at the time the log line is created. In order for that property to show up, you'd have to add it in a middleware before the pino-http one. I do not think there is any other way than |
I tried this:
but it didn't work. After digging into sourcecode I found this:
pino-http/logger.js
Line 44 in a7c82bc
this
refers tores
object, so that would meanres.log
. After changing that code,something
appeared in my output.This is very misleading since all examples suggest to use
req.log
. Also there isn'tres.log
doesn't have typescript type definition.The text was updated successfully, but these errors were encountered: