Skip to content

Commit

Permalink
✨ support logging color for custom logging formats (#1513)
Browse files Browse the repository at this point in the history
  • Loading branch information
amalshaji committed Sep 28, 2021
1 parent 2aef5f8 commit afa53ae
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion middleware/logger/config.go
Expand Up @@ -3,6 +3,7 @@ package logger
import (
"io"
"os"
"strings"
"time"

"github.com/gofiber/fiber/v2"
Expand Down Expand Up @@ -56,6 +57,20 @@ var ConfigDefault = Config{
enableColors: true,
}

// Function to check if the logger format is compatible for coloring
func validCustomFormat(format string) bool {
var validTemplates = []string{"${status}", "${method}"}
if format == "" {
return true
}
for _, template := range validTemplates {
if !strings.Contains(format, template) {
return false
}
}
return true
}

// Helper function to set default values
func configDefault(config ...Config) Config {
// Return default config if nothing provided
Expand All @@ -67,7 +82,7 @@ func configDefault(config ...Config) Config {
cfg := config[0]

// Enable colors if no custom format or output is given
if cfg.Format == "" && cfg.Output == nil {
if validCustomFormat(cfg.Format) && cfg.Output == nil {
cfg.enableColors = true
}

Expand Down

0 comments on commit afa53ae

Please sign in to comment.