-
-
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
Feature Request: Add a Pre-Write Callback to Logger Middleware #3300
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 |
I agree on adding this, got to check other logging framework to see how they name this kind of feature. |
I agree with adding this, but BeforeWrite should be renamed, according to other middlewares in gofiber, similar names are "Next" or "Filter" |
Agree with @liaohongxing this should be named |
If I can throw my own two cents in here, this feels like a job for the decorator pattern |
@bantawa04 Does #3333 cover your use-case? I don't think we need the "logstring" param, since the middleware will write to output after calling "filter()". |
@bantawa04 This feature will be available in Fiber v3 as Example Usage: app := fiber.New()
app.Use(logger.New(logger.Config{
Skip: func(c fiber.Ctx) bool {
// Skip any code below 400
return c.Response().StatusCode() < 400
},
Stream: ErrorLogStream
}))
app.Use(logger.New(logger.Config{
Skip: func(c fiber.Ctx) bool {
// Skip any code other than HTTP 200
return c.Response().StatusCode() != fiber.StatusOK
},
Stream: AccessLogStream
})) |
Currently, the
logger.New
middleware in Fiber writes logs to the specifiedOutput
file automatically. While theDone
callback allows for post-processing of the log string, there's no way to control whether a log entry is written to the file at all before it's written. This makes it difficult to implement fine-grained logging logic, such as selectively logging certain responses based on their content or other criteria.Problem:
I have a use case where I want to log only specific errors (e.g., 4xx/5xx status codes) to an error log file, and potentially other requests (e.g., 2xx) to a separate debug or access log file. The current
Done
callback is called after the log has already been written to theOutput
file. This results in either:Done
to filter logs and write to my error log file, the middleware also writes all logs to theOutput
file, leading to duplication.Done
, I can't filter which log entries are written to which file.Code from my use case
Proposed Solution/Feature request:
I propose adding a new callback function to the
logger.Config
called something likeBeforeWrite
orShouldLog
. This callback would be executed before the middleware writes the log entry to theOutput
file. The callback would receive thefiber.Ctx
and the log string as arguments and would return a boolean value:true
if the log should be written, andfalse
if it should be skipped.Example Usage:
The text was updated successfully, but these errors were encountered: