mirror of https://github.com/gofiber/fiber.git
Improvements for logger middleware (#1645)
* Add NO_COLOR compatibility. * Use /dev/stdout as default output stream. * Fix coloring with custom formats. * Fix tests.pull/1646/head
parent
b74676704d
commit
5e37126352
|
@ -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
|
||||
|
@ -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,
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue