You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, the logger.New middleware in Fiber writes logs to the specified Output file automatically. While the Done 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 the Output file. This results in either:
Double Logging: If I use Done to filter logs and write to my error log file, the middleware also writes all logs to the Output file, leading to duplication.
Inability to Selectively Log: If I don't use Done, I can't filter which log entries are written to which file.
I propose adding a new callback function to the logger.Config called something like BeforeWrite or ShouldLog. This callback would be executed before the middleware writes the log entry to the Output file. The callback would receive the fiber.Ctx and the log string as arguments and would return a boolean value: true if the log should be written, and false if it should be skipped.
Example Usage:
app.Use(logger.New(logger.Config{
TimeFormat: "2006-01-02 15:04:05",
TimeZone: "Local",
Format: `{"timestamp":"${time}", "status":${status}, ...}`, //Log formatOutput: logFile, // General log file (optional)BeforeWrite: func(c*fiber.Ctx, logString []byte) bool {
ifc.Response().StatusCode() >=400 {
// Log errors to the error log fileerrorLogFile.Write(logString) // Example, we can write to different files here.returntrue// Write to the main log file as well (optional). We can return false if we only want to write to the error log.
} elseifc.Response().StatusCode() ==200 {
// Log 200 OK requests to a separate access logaccessLogFile.Write(logString)
returntrue// Write to the main log file as well (optional). We can return false if we only want to write to the access log.
}
returnfalse// Don't write other requests to the main log file.
},
Done: func(c*fiber.Ctx, logString []byte) {
// Any post-processing
},
}))
The text was updated successfully, but these errors were encountered:
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
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: