From 814332340d39003f7a0e6032fbfc171bd1e5ce0b Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 23 Apr 2020 13:31:58 -0400 Subject: [PATCH 1/3] feat: add ability to override the debugPrint statement This allows users to use a single logger within their application for all printing, regardless of level. --- debug.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/debug.go b/debug.go index c66ca4408f..dbcd7af4aa 100644 --- a/debug.go +++ b/debug.go @@ -23,6 +23,9 @@ func IsDebugging() bool { // DebugPrintRouteFunc indicates debug log output format. var DebugPrintRouteFunc func(httpMethod, absolutePath, handlerName string, nuHandlers int) +// DebugPrintFunc indicates debug log output format. +var DebugPrintFunc func(format string, values ...interface{}) + func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) { if IsDebugging() { nuHandlers := len(handlers) @@ -49,10 +52,14 @@ func debugPrintLoadTemplate(tmpl *template.Template) { func debugPrint(format string, values ...interface{}) { if IsDebugging() { - if !strings.HasSuffix(format, "\n") { - format += "\n" + if DebugPrintFunc == nil { + if !strings.HasSuffix(format, "\n") { + format += "\n" + } + fmt.Fprintf(DefaultWriter, "[GIN-debug] "+format, values...) + } else { + DebugPrintFunc(format, values...) } - fmt.Fprintf(DefaultWriter, "[GIN-debug] "+format, values...) } } From 671c94f23e0506084bc95565444c8ff280ae97b8 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Sat, 3 Jun 2023 13:28:41 -0400 Subject: [PATCH 2/3] chore: make the code more readable, as per review comment --- debug.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/debug.go b/debug.go index dbcd7af4aa..ae88482b23 100644 --- a/debug.go +++ b/debug.go @@ -51,16 +51,19 @@ func debugPrintLoadTemplate(tmpl *template.Template) { } func debugPrint(format string, values ...interface{}) { - if IsDebugging() { - if DebugPrintFunc == nil { - if !strings.HasSuffix(format, "\n") { - format += "\n" - } - fmt.Fprintf(DefaultWriter, "[GIN-debug] "+format, values...) - } else { - DebugPrintFunc(format, values...) - } + if !IsDebugging() { + return + } + + if DebugPrintFunc != nil { + DebugPrintFunc(format, values...) + return + } + + if !strings.HasSuffix(format, "\n") { + format += "\n" } + fmt.Fprintf(DefaultWriter, "[GIN-debug] "+format, values...) } func getMinVer(v string) (uint64, error) { From 5f3d2526cdcc30113b7e94b690b4c0c68d79b9b8 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 5 Jun 2023 11:39:16 -0400 Subject: [PATCH 3/3] fix: use tab instead of space for indentation --- debug.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/debug.go b/debug.go index ee7b84ef15..1761fe325e 100644 --- a/debug.go +++ b/debug.go @@ -62,8 +62,7 @@ func debugPrint(format string, values ...any) { if !strings.HasSuffix(format, "\n") { format += "\n" - } - + } fmt.Fprintf(DefaultWriter, "[GIN-debug] "+format, values...) }