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

✨ support logging color for custom logging formats #1513

Merged
merged 1 commit into from Sep 28, 2021
Merged
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
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