Request-scoped logger #5220
fr3fou
started this conversation in
Feature Requests & Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Motivation
pinojs' child loggers are incredibly useful in the context of request-scoped logging. They allow you to provide extra information with every subsequent log that uses the same logger.
One example of this is to attach a
requestId
field to a child logger in a middleware before every request and then have each log automatically "inherit" the samerequestId
. This allows for easy debugging by filtering all relevant logs by the samerequestId
. This way you can see all your debug / info / error logs and see the entire trace of a user action. Another example would be to attachuserId
ortenantId
.Problem
Unfortunately, Payload doesn't allow a way to override the logger for a single request. Your middleware would have to override the
req.payload.logger
field which (due to the singleton nature of Payload) will override the logger for every concurrent request.A workaround would involve:
req
objectThis works for logs that the end user of Payload might be making, but it doesn't handle any logs that Payload itself (or any plugin for that matter) might be making. For example, Payload provides a built in
errorHandler
middleware:payload/packages/payload/src/initHTTP.ts
Lines 87 to 88 in 8104fac
If you want to trace an error log all the way back to the initial HTTP request that caused it, you wouldn't be able to, as that doesn't use your custom child logger.
Solutions
There're 2 solutions that come to mind:
getLogger(req: PayloadRequest): pino.Logger
function that would then be called by Payload on every request instead of using the base loggerrequestId
to every log – this wouldn't cover all use cases but it'd be good enoughBeta Was this translation helpful? Give feedback.
All reactions