-
The moment a request comes in, I'd like to generate a traceId, which I'll include in any logs my application generates. Here's how I do this with a different API library (TRPC):
I set up my logger to read from traceIdStorage so I can include that data in any logs I send. I'd like to do the same with express-zod-api, but the middlewares are a little different - there's no |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 12 replies
-
Think I've found it:
However, it would be handy to be able to do it later, where I have figured out who my user is (then I can attach usernames etc to my logger context) |
Beta Was this translation helpful? Give feedback.
-
Hello, @crummy There is a dedicated article on that topic: Implying that your logger has a import { createConfig } from "express-zod-api";
import { randomUUID } from "node:crypto";
const config = createConfig({
// logger: ...,
childLoggerProvider: ({ parent, request }) =>
parent.child({ requestId: randomUUID() }),
}); Basically you simply create another instance of the logger inside that function, that somehow displays the request ID. |
Beta Was this translation helpful? Give feedback.
Hello, @crummy
There is a dedicated article on that topic:
https://github.com/RobinTail/express-zod-api?tab=readme-ov-file#child-logger
Implying that your logger has a
.child()
method:Basically you simply create another instance of the logger inside that function, that somehow displays the request ID.
So that you don't even need the storage — this approach enables a dedicated instance of the logger for the individual request.
I hope it helps.