Comments (10)
Can you add an example to reproduce the problem you are facing?
from pino-http.
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 });
});
req.log.info("some message")
will have something: true
in its output, however, the request completed
will not have it, because that one relays on res
.
I hope this make it more clear.
from pino-http.
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)
from pino-http.
for injecting values on a per request basis use the serializers
from pino-http.
Value changes per request. I tried setting that value on req
object, however that didn't show up in serializers from req.raw
.
from pino-http.
I think it would be helpful to have a full example of what you are trying to do.
from pino-http.
#74 (comment)
This is almost the same thing as I'm doing, but here goes:
1.
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:
- I would need to do
req.log = res.log = res.log.child({ originalRoute });
to make it work, which is misleading and not obvious if you don't take a look at sourcecode. Also, as suggested, not a good pattern. - In this case
req.raw
doesn't contain myoriginalRoute
, not sure why tho. Maybe.child
creates copy of original object? 🤔
from pino-http.
May I get a full example that I can run? Ideally without express (or maybe this is express-only).
from pino-http.
Here it is: https://github.com/GuskiS/pino-express-failure
from pino-http.
In this case req.raw doesn't contain my originalRoute, not sure why tho. Maybe .child creates copy of original object?
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 req.log = res.log = res.log.child({ originalRoute });
tbh.
You could potentially disable autoLogging
and log after you have set that value.
from pino-http.
Related Issues (20)
- use as middleware example HOT 2
- Documentation: res.log() is only mentioned in quietReqLogger? HOT 8
- [Documentation error]: The value assignment is set before the declaration
- reading 'stringifySym' doesn't work with next.js HOT 5
- IncomingMessage and ServerMessage typings are too specific HOT 2
- Updating for v10 HOT 2
- How can I put context in request completed log (when autoLogging is true) ?
- Release next version HOT 1
- node:util module not found HOT 6
- nestedKey now working properly HOT 1
- Message "request completed" when request not completed HOT 1
- `wrapChild` doesn't pass on options to the created child logger as expected HOT 3
- How best to handle extending req.log? HOT 6
- customProps causes TypeError: logger[stringifySym] is not a function HOT 17
- Type 'Opts' does not satisfy the constraint 'string'.ts(2344) HOT 5
- TypeScript error when using customLevels on pino parent instance HOT 1
- Updating for v9 HOT 3
- Log req only on 1st log from the request HOT 2
- Unable to remove whole req object from response logs HOT 3
- Undocumented Breaking Changes for 9.0.0 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pino-http.