Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/middleware/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ app.Use(logger.New(logger.Config{
DisableColors: true,
}))

// Force the use of colors
app.Use(logger.New(logger.Config{
ForceColors: true,
}))

// Use predefined formats
app.Use(logger.New(logger.Config{
Format: logger.FormatCommon,
Expand Down Expand Up @@ -166,6 +171,7 @@ Writing to os.File is goroutine-safe, but if you are using a custom Stream that
| Stream | `io.Writer` | Stream is a writer where logs are written. | `os.Stdout` |
| LoggerFunc | `func(c fiber.Ctx, data *Data, cfg Config) error` | Custom logger function for integration with logging libraries (Zerolog, Zap, Logrus, etc). Defaults to Fiber's default logger if not defined. | `see default_logger.go defaultLoggerInstance` |
| DisableColors | `bool` | DisableColors defines if the logs output should be colorized. | `false` |
| ForceColors | `bool` | ForceColors defines if the logs output should be colorized even when the output is not a terminal. | `false` |

## Default Config

Expand Down
7 changes: 6 additions & 1 deletion middleware/logger/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ type Config struct {
// Default: false
DisableColors bool

// ForceColors forces the colors to be enabled even if the output is not a terminal
//
// Default: false
ForceColors bool

enableColors bool
enableLatency bool
}
Expand Down Expand Up @@ -174,7 +179,7 @@ func configDefault(config ...Config) Config {
}

// Enable colors if no custom format or output is given
if !cfg.DisableColors && cfg.Stream == ConfigDefault.Stream {
if (!cfg.DisableColors && cfg.Stream == ConfigDefault.Stream) || cfg.ForceColors {
cfg.enableColors = true
}

Expand Down
2 changes: 1 addition & 1 deletion middleware/logger/default_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func beforeHandlerFunc(cfg Config) {
// If colors are enabled, check terminal compatibility
if cfg.enableColors {
cfg.Stream = colorable.NewColorableStdout()
if os.Getenv("TERM") == "dumb" || os.Getenv("NO_COLOR") == "1" || (!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd())) {
if !cfg.ForceColors && (os.Getenv("TERM") == "dumb" || os.Getenv("NO_COLOR") == "1" || (!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()))) {
cfg.Stream = colorable.NewNonColorable(os.Stdout)
}
}
Expand Down
50 changes: 50 additions & 0 deletions middleware/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,32 @@ func Test_Logger_EnableColors(t *testing.T) {
require.EqualValues(t, 1, *o)
}

// go test -run Test_Logger_ForceColors
func Test_Logger_ForceColors(t *testing.T) {
t.Parallel()
buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf)

app := fiber.New()

app.Use(New(Config{
Format: "${ip}${status}${method}${path}${error}\n",
Stream: buf,
DisableColors: true,
ForceColors: true,
}))

// Alias colors
colors := app.Config().ColorScheme

resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
require.NoError(t, err)
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)

expected := fmt.Sprintf("0.0.0.0%s404%s%sGET%s/%sCannot GET /%s\n", colors.Yellow, colors.Reset, colors.Cyan, colors.Reset, colors.Red, colors.Reset)
require.Equal(t, expected, buf.String())
}

// go test -v -run=^$ -bench=Benchmark_Logger$ -benchmem -count=4
func Benchmark_Logger(b *testing.B) {
b.Run("NoMiddleware", func(bb *testing.B) {
Expand Down Expand Up @@ -1064,6 +1090,18 @@ func Benchmark_Logger(b *testing.B) {
benchmarkSetup(bb, app, "/")
})

b.Run("DefaultFormatForceColors", func(bb *testing.B) {
app := fiber.New()
app.Use(New(Config{
Stream: io.Discard,
ForceColors: true,
}))
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("Hello, World!")
})
benchmarkSetup(bb, app, "/")
})

b.Run("DefaultFormatWithFiberLog", func(bb *testing.B) {
app := fiber.New()
logger := fiberlog.DefaultLogger()
Expand Down Expand Up @@ -1245,6 +1283,18 @@ func Benchmark_Logger_Parallel(b *testing.B) {
benchmarkSetupParallel(bb, app, "/")
})

b.Run("DefaultFormatForceColors", func(bb *testing.B) {
app := fiber.New()
app.Use(New(Config{
Stream: io.Discard,
ForceColors: true,
}))
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("Hello, World!")
})
benchmarkSetupParallel(bb, app, "/")
})

b.Run("WithTagParameter", func(bb *testing.B) {
app := fiber.New()
app.Use(New(Config{
Expand Down