Skip to content

Commit 5a75475

Browse files
committed
Fix debug logger side effects
A previous improvement in signing the package name with debug log entries did actually pollute the global logger settings. Now fixed with a private debug logger for this package.
1 parent f8f9c59 commit 5a75475

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

debug.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,22 @@ var (
2626
// Debug is true when the SWAGGER_DEBUG env var is not empty.
2727
// It enables a more verbose logging of validators.
2828
Debug = os.Getenv("SWAGGER_DEBUG") != ""
29+
// validateLogger is a debug logger for this package
30+
validateLogger *log.Logger
2931
)
3032

3133
func init() {
3234
debugOptions()
3335
}
3436

3537
func debugOptions() {
36-
if Debug {
37-
//log.SetFlags(log.LstdFlags | log.Lshortfile)
38-
log.SetFlags(log.LstdFlags)
39-
log.SetPrefix("validate:")
40-
}
38+
validateLogger = log.New(os.Stdout, "validate:", log.LstdFlags)
4139
}
4240

4341
func debugLog(msg string, args ...interface{}) {
4442
// A private, trivial trace logger, based on go-openapi/spec/expander.go:debugLog()
4543
if Debug {
4644
_, file1, pos1, _ := runtime.Caller(1)
47-
log.Printf("%s:%d: %s", filepath.Base(file1), pos1, fmt.Sprintf(msg, args...))
45+
validateLogger.Printf("%s:%d: %s", filepath.Base(file1), pos1, fmt.Sprintf(msg, args...))
4846
}
4947
}

debug_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package validate
1616

1717
import (
1818
"io/ioutil"
19-
"log"
2019
"os"
2120
"sync"
2221
"testing"
@@ -37,23 +36,28 @@ func TestDebug(t *testing.T) {
3736
tmpName := tmpFile.Name()
3837
defer func() {
3938
Debug = false
40-
log.SetOutput(os.Stdout)
4139
// mutex for -race
4240
logMutex.Unlock()
4341
os.Remove(tmpName)
4442
}()
4543

4644
// mutex for -race
4745
logMutex.Lock()
48-
log.SetOutput(tmpFile)
4946
Debug = true
5047
debugOptions()
48+
defer func() {
49+
validateLogger.SetOutput(os.Stdout)
50+
}()
51+
52+
validateLogger.SetOutput(tmpFile)
53+
5154
debugLog("A debug")
5255
Debug = false
5356
tmpFile.Close()
57+
5458
flushed, _ := os.Open(tmpName)
5559
buf := make([]byte, 500)
5660
flushed.Read(buf)
57-
log.SetOutput(os.Stdout)
61+
validateLogger.SetOutput(os.Stdout)
5862
assert.Contains(t, string(buf), "A debug")
5963
}

0 commit comments

Comments
 (0)