Skip to content

Commit

Permalink
Improvements for logger middleware (#1645)
Browse files Browse the repository at this point in the history
* Add NO_COLOR compatibility.

* Use /dev/stdout as default output stream.

* Fix coloring with custom formats.

* Fix tests.
  • Loading branch information
efectn committed Dec 6, 2021
1 parent b746767 commit 5e37126
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
10 changes: 5 additions & 5 deletions middleware/logger/config.go
Expand Up @@ -38,7 +38,7 @@ type Config struct {

// Output is a writter where logs are written
//
// Default: os.Stderr
// Default: os.Stdout
Output io.Writer

enableColors bool
Expand All @@ -53,7 +53,7 @@ var ConfigDefault = Config{
TimeFormat: "15:04:05",
TimeZone: "Local",
TimeInterval: 500 * time.Millisecond,
Output: os.Stderr,
Output: os.Stdout,
enableColors: true,
}

Expand All @@ -64,11 +64,11 @@ func validCustomFormat(format string) bool {
return true
}
for _, template := range validTemplates {
if !strings.Contains(format, template) {
return false
if strings.Contains(format, template) {
return true
}
}
return true
return false
}

// Helper function to set default values
Expand Down
14 changes: 10 additions & 4 deletions middleware/logger/logger.go
Expand Up @@ -118,9 +118,9 @@ func New(config ...Config) fiber.Handler {

// If colors are enabled, check terminal compatibility
if cfg.enableColors {
cfg.Output = colorable.NewColorableStderr()
if os.Getenv("TERM") == "dumb" || (!isatty.IsTerminal(os.Stderr.Fd()) && !isatty.IsCygwinTerminal(os.Stderr.Fd())) {
cfg.Output = colorable.NewNonColorable(os.Stderr)
cfg.Output = colorable.NewColorableStdout()
if os.Getenv("TERM") == "dumb" || os.Getenv("NO_COLOR") == "1" || (!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd())) {
cfg.Output = colorable.NewNonColorable(os.Stdout)
}
}
errPadding := 15
Expand Down Expand Up @@ -174,7 +174,7 @@ func New(config ...Config) fiber.Handler {
buf := bytebufferpool.Get()

// Default output when no custom Format or io.Writer is given
if cfg.enableColors {
if cfg.enableColors && cfg.Format == ConfigDefault.Format {
// Format error if exist
formatErr := ""
if chainErr != nil {
Expand Down Expand Up @@ -238,12 +238,18 @@ func New(config ...Config) fiber.Handler {
case TagRoute:
return buf.WriteString(c.Route().Path)
case TagStatus:
if cfg.enableColors {
return buf.WriteString(fmt.Sprintf("%s %3d %s", statusColor(c.Response().StatusCode()), c.Response().StatusCode(), cReset))
}
return appendInt(buf, c.Response().StatusCode())
case TagResBody:
return buf.Write(c.Response().Body())
case TagQueryStringParams:
return buf.WriteString(c.Request().URI().QueryArgs().String())
case TagMethod:
if cfg.enableColors {
return buf.WriteString(fmt.Sprintf("%s %-7s %s", methodColor(c.Method()), c.Method(), cReset))
}
return buf.WriteString(c.Method())
case TagBlack:
return buf.WriteString(cBlack)
Expand Down

0 comments on commit 5e37126

Please sign in to comment.