Skip to content

Commit 1b3c085

Browse files
authored
chore(debug): add ability to override the debugPrint statement (#2337)
* 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. * chore: make the code more readable, as per review comment * fix: use tab instead of space for indentation
1 parent ac5e84d commit 1b3c085

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

debug.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ func IsDebugging() bool {
2323
// DebugPrintRouteFunc indicates debug log output format.
2424
var DebugPrintRouteFunc func(httpMethod, absolutePath, handlerName string, nuHandlers int)
2525

26+
// DebugPrintFunc indicates debug log output format.
27+
var DebugPrintFunc func(format string, values ...interface{})
28+
2629
func debugPrintRoute(httpMethod, absolutePath string, handlers HandlersChain) {
2730
if IsDebugging() {
2831
nuHandlers := len(handlers)
@@ -48,12 +51,19 @@ func debugPrintLoadTemplate(tmpl *template.Template) {
4851
}
4952

5053
func debugPrint(format string, values ...any) {
51-
if IsDebugging() {
52-
if !strings.HasSuffix(format, "\n") {
53-
format += "\n"
54-
}
55-
fmt.Fprintf(DefaultWriter, "[GIN-debug] "+format, values...)
54+
if !IsDebugging() {
55+
return
56+
}
57+
58+
if DebugPrintFunc != nil {
59+
DebugPrintFunc(format, values...)
60+
return
61+
}
62+
63+
if !strings.HasSuffix(format, "\n") {
64+
format += "\n"
5665
}
66+
fmt.Fprintf(DefaultWriter, "[GIN-debug] "+format, values...)
5767
}
5868

5969
func getMinVer(v string) (uint64, error) {

0 commit comments

Comments
 (0)