fiber
fiber copied to clipboard
🤗 [Question]: About fiber v3 log
Question Description
The Fiber v3 version provides a log package. When the project is initialized, the RequestID middleware is used. Why isn't the request-id carried by default in the log.WithContext(ctx)? Currently, it looks like log.WithContext(ctx).Info and log.Info are the same.
Code Snippet (optional)
package main
import (
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/log"
"github.com/gofiber/fiber/v3/middleware/requestid"
)
func main() {
app := fiber.New()
app.Use(requestid.New())
app.Get("/", func(c fiber.Ctx) error {
log.WithContext(c).Info("start...")
// 模拟业务逻辑调用
businessLogic(c)
log.WithContext(c).Info("end...")
return c.SendString("Hello, Fiber!")
})
log.Fatal(app.Listen(":3000"))
}
func businessLogic(c fiber.Ctx) {
log.WithContext(c).Debug("businessLogic...")
}
Checklist:
- [x] I agree to follow Fiber's Code of Conduct.
- [x] I have checked for existing issues that describe my questions prior to opening this one.
- [x] I understand that improperly formatted questions may be closed without explanation.
@shangyin2024 The log package is standalone, for getting the requestid you have to call requestid.FromContext(c) and pass that as a field to the logger. You can also use the logger middleware, there's example of using RequestID in the docs here: https://docs.gofiber.io/next/middleware/logger#examples
@shangyin2024 I'm working on a potential fix for this