-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Closed
Labels
Description
Feature Description
Refactor the logger middleware in Fiber to adher to the "Don't Repeat Yourself" (DRY) principle. During recent discussions #2561, it was identified that the current implementation involves duplicated code for custom and default logger functions.
Additional Context (optional)
Code Refactoring: The primary focus is to eliminate code duplication by consolidating the common logic shared between custom and default logger functions. This not only improves code maintainability but also ensures consistent behavior across different configurations.
Code Snippet (optional)
// Default output when no custom Format or io.Writer is given
if cfg.Format == ConfigDefault.Format {
// Format error if exist
formatErr := ""
if cfg.enableColors {
if chainErr != nil {
formatErr = colors.Red + " | " + chainErr.Error() + colors.Reset
}
_, _ = buf.WriteString( //nolint:errcheck // This will never fail
fmt.Sprintf("%s |%s %3d %s| %7v | %15s |%s %-7s %s| %-"+errPaddingStr+"s %s\n",
timestamp.Load().(string),
statusColor(c.Response().StatusCode(), colors), c.Response().StatusCode(), colors.Reset,
data.Stop.Sub(data.Start).Round(time.Millisecond),
c.IP(),
methodColor(c.Method(), colors), c.Method(), colors.Reset,
c.Path(),
formatErr,
),
)
} else {
if chainErr != nil {
formatErr = " | " + chainErr.Error()
}
_, _ = buf.WriteString( //nolint:errcheck // This will never fail
fmt.Sprintf("%s | %3d | %7v | %15s | %-7s | %-"+errPaddingStr+"s %s\n",
timestamp.Load().(string),
c.Response().StatusCode(),
data.Stop.Sub(data.Start).Round(time.Millisecond),
c.IP(),
c.Method(),
c.Path(),
formatErr,
),
)
}
// Write buffer to output
_, _ = cfg.Output.Write(buf.Bytes()) //nolint:errcheck // This will never fail
if cfg.Done != nil {
cfg.Done(c, buf.Bytes())
}
// Put buffer back to pool
bytebufferpool.Put(buf)
// End chain
return nil
}Checklist:
- I agree to follow Fiber's Code of Conduct.
- I have checked for existing issues that describe my suggestion prior to opening this one.
- I understand that improperly formatted feature requests may be closed without explanation.
Reactions are currently unavailable