From afa53ae1f6b9a8dc7543a6377139fa205104986b Mon Sep 17 00:00:00 2001 From: Amal Shaji <18011385+amalshaji@users.noreply.github.com> Date: Tue, 28 Sep 2021 01:11:44 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20support=20logging=20color=20for=20c?= =?UTF-8?q?ustom=20logging=20formats=20(#1513)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware/logger/config.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/middleware/logger/config.go b/middleware/logger/config.go index c5dd48a751..0ab75d6fd5 100644 --- a/middleware/logger/config.go +++ b/middleware/logger/config.go @@ -3,6 +3,7 @@ package logger import ( "io" "os" + "strings" "time" "github.com/gofiber/fiber/v2" @@ -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 @@ -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 }