Next.js Discord

Discord Forum

Adding request ID to logger

Unanswered
Blue Bunting posted this in #help-forum
Open in Discord
Blue BuntingOP
I am using pino for logging and I want to add the request ID to the logger in the API. This is not difficult when operating solely within a handler as I can just instantiate a child logger, but sometimes the handler calls out to global functions which have their own log statements.

To remedy this I am trying to get some of the metadata from the tracing context when using the experimental instrumentation hook. Ideally the logger could call context.getActive() and then read any interesting tags or baggage from the context. But getActive() does not return any context.

There may also be a better way to do this without having to reach for the new instrumentation feature. Does anyone know of a clean pattern for automatically logging with context without having to param-drill said context to every handler and function?

0 Replies