-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
📝 docs: Document usage of Custom Tags in Logger middleware #3446
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -933,6 +933,77 @@ | |
|
|
||
| </details> | ||
|
|
||
| #### Logging Middleware Values (e.g., Request ID) | ||
|
|
||
| In Fiber v3, middleware (like `requestid`) now stores values in the request context using unexported keys of custom types. This aligns with Go's context best practices to prevent key collisions between packages. | ||
|
|
||
| As a result, directly accessing these values using string keys with `c.Locals("your_key")` or in the logger format string with `${locals:your_key}` (e.g., `${locals:requestid}`) will no longer work for values set by such middleware. | ||
|
|
||
| **Recommended Solution: `CustomTags`** | ||
|
|
||
| The cleanest and most maintainable way to include these middleware-specific values in your logs is by using the `CustomTags` option in the logger middleware configuration. This allows you to define a custom function to retrieve the value correctly from the context. | ||
|
|
||
| <details> | ||
| <summary>Example: Logging Request ID with CustomTags</summary> | ||
|
|
||
| ```go | ||
| package main | ||
|
|
||
| import ( | ||
| "github.com/gofiber/fiber/v3" | ||
| "github.com/gofiber/fiber/v3/middleware/logger" | ||
| "github.com/gofiber/fiber/v3/middleware/requestid" | ||
| ) | ||
|
|
||
| func main() { | ||
| app := fiber.New() | ||
|
|
||
| // Ensure requestid middleware is used before the logger | ||
| app.Use(requestid.New()) | ||
|
|
||
| app.Use(logger.New(logger.Config{ | ||
| CustomTags: map[string]logger.LogFunc{ | ||
| "requestid": func(output logger.Buffer, c fiber.Ctx, data *logger.Data, extraParam string) (int, error) { | ||
| // Retrieve the request ID using the middleware's specific function | ||
| return output.WriteString(requestid.FromContext(c)) | ||
| }, | ||
| }, | ||
| // Use the custom tag in your format string | ||
| Format: "[${time}] ${ip} - ${requestid} - ${status} ${method} ${path}\n", | ||
| })) | ||
|
|
||
| app.Get("/", func(c fiber.Ctx) error { | ||
| return c.SendString("Hello, World!") | ||
| }) | ||
|
|
||
| app.Listen(":3000") | ||
| } | ||
| ``` | ||
|
Check failure on line 981 in docs/whats_new.md
|
||
| </details> | ||
|
|
||
| **Alternative: Manually Copying to `Locals`** | ||
|
|
||
| If you have existing logging patterns that rely on `c.Locals` or prefer to manage these values in `Locals` for other reasons, you can manually copy the value from the context to `c.Locals` in a preceding middleware: | ||
|
|
||
| <details> | ||
| <summary>Example: Manually setting requestid in Locals</summary> | ||
|
|
||
| ```go | ||
| app.Use(requestid.New()) // Request ID middleware | ||
| app.Use(func(c fiber.Ctx) error { | ||
| // Manually copy the request ID to Locals | ||
| c.Locals("requestid", requestid.FromContext(c)) | ||
| return c.Next() | ||
| }) | ||
| app.Use(logger.New(logger.Config{ | ||
| // Now ${locals:requestid} can be used, but CustomTags is generally preferred | ||
| Format: "[${time}] ${ip} - ${locals:requestid} - ${status} ${method} ${path}\n", | ||
| })) | ||
| ``` | ||
|
Check failure on line 1002 in docs/whats_new.md
|
||
| </details> | ||
|
|
||
| Both approaches ensure your logger can access these values while respecting Go's context practices. | ||
|
|
||
| The `Skip` is a function to determine if logging is skipped or written to `Stream`. | ||
|
|
||
| <details> | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.