-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
🤗 [Question]: Is there any way to log request id on handler? #3169
Comments
Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
this way needs fiber context at every situation.. right? |
yes as middleware it works directly with the fiber context the question is whether you need a req/res logger or just want to log additional information the variant I have shown you is an independent middleware for logging the req/res data |
my question is about that i want to log additional information, not in req/res logger |
ok, how do you generate this requestID ? with the middleware? |
you can always inject a middleware that logs this id or does other things app.Use(func(c *fiber.Ctx) error {
// log requestId
log.Println("requestId:", c.Locals("requestid")) // requestId from the locals(context) object - https://docs.gofiber.io/api/middleware/requestid#config
log.Println("requestId:", c.Get(fiber.HeaderXRequestID)) // requestId from the request header
return c.Next()
}) |
Instead of using middleware, if I want to log additional information in an API controller or service, do I always need the Fiber context to get the request ID? If that's the case, it feels inconvenient to pass the context to the logger each time like this: sugarLogger.With(
zap.String("requestID", context.Get(fiber.HeaderXRequestID)),
).Infof("Printf-style logging with context: %s", "additional info") I'm looking for a different approach because it seems cumbersome to log this way. The reason I want to implement this is to distinguish logs from the same request when multiple requests come in simultaneously. |
all request and response information can be found in the fiber context app.Use(func(c *fiber.Ctx) error {
// extend and (TODO) store this logger
zap.With(zap.String("requestId", c.Get(fiber.HeaderXRequestID)).Info("requestId"))
return c.Next()
}) |
I think this way is best for my situation, I'll try it thank you!! |
Question Description
i'm new to go fiber and develop with uber zap, fx.
The problem is i know how to log request id in middleware logger, But I made a another logger to use on handler and using it by dependency injection.
how can i log request id on second logger?
i guess that i can make a log function to accept
fiber context
as parameter, and extract request id from it,but I wish there were other options than this, like in middleware, which can be pre-set
Code Snippet (optional)
Checklist:
The text was updated successfully, but these errors were encountered: