forked from ipfs/go-log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
49 lines (40 loc) · 1.03 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package log
import (
"sync"
"testing"
"go.uber.org/zap"
)
var (
// reuse the same logger across all tests
testingLoggerMtx = sync.Mutex{}
testingLogger *zap.SugaredLogger
)
// Type check
var _ StandardLogger = (*zap.SugaredLogger)(nil)
// NopLogger returns a no-op Logger. It never writes out logs or internal errors.
func NopLogger() *zap.SugaredLogger {
return zap.NewNop().Sugar()
}
// TestingLogger returns a Logger which writes to STDOUT if test(s) are being
// run with the verbose (-v) flag, NopLogger otherwise.
//
// NOTE:
// - A call to NewTestingLogger() must be made inside a test (not in the init func)
// because verbose flag only set at the time of testing.
func TestingLogger() *zap.SugaredLogger {
testingLoggerMtx.Lock()
defer testingLoggerMtx.Unlock()
if testingLogger != nil {
return testingLogger
}
if testing.Verbose() {
if logger, err := zap.NewDevelopmentConfig().Build(); err != nil {
panic(err)
} else {
return logger.Sugar()
}
} else {
testingLogger = NopLogger()
}
return testingLogger
}