Use DefaultWriter and DefaultErrorWriter for debug messages (#1891)

Aligns behaviour according to documentation.
This commit is contained in:
Uwe Dauernheim 2019-05-10 08:03:25 +02:00 committed by Bo-Yi Wu
parent 5a7e3095b2
commit 04eecb1283
2 changed files with 10 additions and 9 deletions

View File

@ -8,7 +8,6 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"html/template" "html/template"
"os"
"runtime" "runtime"
"strconv" "strconv"
"strings" "strings"
@ -54,7 +53,7 @@ func debugPrint(format string, values ...interface{}) {
if !strings.HasSuffix(format, "\n") { if !strings.HasSuffix(format, "\n") {
format += "\n" format += "\n"
} }
fmt.Fprintf(os.Stderr, "[GIN-debug] "+format, values...) fmt.Fprintf(DefaultWriter, "[GIN-debug] "+format, values...)
} }
} }
@ -98,6 +97,8 @@ at initialization. ie. before any route is registered or the router is listening
func debugPrintError(err error) { func debugPrintError(err error) {
if err != nil { if err != nil {
debugPrint("[ERROR] %v\n", err) if IsDebugging() {
fmt.Fprintf(DefaultErrorWriter, "[GIN-debug] [ERROR] %v\n", err)
}
} }
} }

View File

@ -111,15 +111,15 @@ func captureOutput(t *testing.T, f func()) string {
if err != nil { if err != nil {
panic(err) panic(err)
} }
stdout := os.Stdout defaultWriter := DefaultWriter
stderr := os.Stderr defaultErrorWriter := DefaultErrorWriter
defer func() { defer func() {
os.Stdout = stdout DefaultWriter = defaultWriter
os.Stderr = stderr DefaultErrorWriter = defaultErrorWriter
log.SetOutput(os.Stderr) log.SetOutput(os.Stderr)
}() }()
os.Stdout = writer DefaultWriter = writer
os.Stderr = writer DefaultErrorWriter = writer
log.SetOutput(writer) log.SetOutput(writer)
out := make(chan string) out := make(chan string)
wg := new(sync.WaitGroup) wg := new(sync.WaitGroup)