Skip to content

Commit

Permalink
♻️: logger/middleware to Eliminate Code Duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
Skyenought committed Aug 22, 2023
1 parent 7850cde commit 4ac4024
Showing 1 changed file with 21 additions and 28 deletions.
49 changes: 21 additions & 28 deletions middleware/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,40 +139,33 @@ func New(config ...Config) fiber.Handler {

// Default output when no custom Format or io.Writer is given
if cfg.Format == ConfigDefault.Format {
// Format error if exist
// Format error if it exists
formatErr := ""
if cfg.enableColors {
if chainErr != nil {
if chainErr != nil {
if cfg.enableColors {
formatErr = colors.Red + " | " + chainErr.Error() + colors.Reset
}
_, _ = buf.WriteString( //nolint:errcheck // This will never fail
fmt.Sprintf("%s |%s %3d %s| %13v | %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),
c.IP(),
methodColor(c.Method(), colors), c.Method(), colors.Reset,
c.Path(),
formatErr,
),
)
} else {
if chainErr != nil {
} else {
formatErr = " | " + chainErr.Error()
}
_, _ = buf.WriteString( //nolint:errcheck // This will never fail
fmt.Sprintf("%s | %3d | %13v | %15s | %-7s | %-"+errPaddingStr+"s %s\n",
timestamp.Load().(string),
c.Response().StatusCode(),
data.Stop.Sub(data.Start),
c.IP(),
c.Method(),
c.Path(),
formatErr,
),
)
}

// Construct the log format
logFormat := "%s |%s %3d %s| %7v | %15s |%s %-7s %s| %-" + errPaddingStr + "s %s\n"
if !cfg.enableColors {
logFormat = "%s | %3d | %7v | %15s | %-7s | %-" + errPaddingStr + "s %s\n"
}

// Write log entry to buffer
_, _ = buf.WriteString(fmt.Sprintf(logFormat,
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,
))

// Write buffer to output
_, _ = cfg.Output.Write(buf.Bytes()) //nolint:errcheck // This will never fail

Expand Down

0 comments on commit 4ac4024

Please sign in to comment.