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
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 givenifcfg.Format==ConfigDefault.Format {
// Format error if existformatErr:=""ifcfg.enableColors {
ifchainErr!=nil {
formatErr=colors.Red+" | "+chainErr.Error() +colors.Reset
}
_, _=buf.WriteString( //nolint:errcheck // This will never failfmt.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 {
ifchainErr!=nil {
formatErr=" | "+chainErr.Error()
}
_, _=buf.WriteString( //nolint:errcheck // This will never failfmt.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 failifcfg.Done!=nil {
cfg.Done(c, buf.Bytes())
}
// Put buffer back to poolbytebufferpool.Put(buf)
// End chainreturnnil
}
sixcolors
changed the title
🚀 [Refactor]: logger/middleware to Eliminate Code Duplication
♻️ [Refactor]: logger/middleware to Eliminate Code Duplication
Aug 17, 2023
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)
Checklist:
The text was updated successfully, but these errors were encountered: