Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements for logger middleware #1645

Merged
merged 4 commits into from Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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