forked from eoscanada/eos-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
79 lines (64 loc) · 1.5 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package eos
import (
"fmt"
"go.uber.org/zap"
)
var coreLog = zap.NewNop()
var encoderLog = zap.NewNop()
var decoderLog = zap.NewNop()
var abiEncoderLog = zap.NewNop()
var abiDecoderLog = zap.NewNop()
var loggingEnabled = false
func EnableDebugLogging(l *zap.Logger) {
coreLog = l
encoderLog = l
decoderLog = l
abiEncoderLog = l
abiDecoderLog = l
loggingEnabled = true
}
func EnableCoreLogging() {
coreLog = newLogger(false)
enableLogging(coreLog)
}
func EnableEncoderLogging() {
encoderLog = newLogger(false)
enableLogging(encoderLog)
}
func EnableDecoderLogging() {
decoderLog = newLogger(false)
enableLogging(decoderLog)
}
func EnableABIEncoderLogging() {
abiEncoderLog = newLogger(false)
enableLogging(abiEncoderLog)
}
func EnableABIDecoderLogging() {
abiDecoderLog = newLogger(false)
enableLogging(abiDecoderLog)
}
func enableLogging(logger *zap.Logger) {
if loggingEnabled == false {
logger.Warn("Enabling logs. Expect performance hits for high throughput")
loggingEnabled = true
}
}
type logStringerFunc func() string
func (f logStringerFunc) String() string { return f() }
func typeField(field string, v interface{}) zap.Field {
return zap.Stringer(field, logStringerFunc(func() string {
return fmt.Sprintf("%T", v)
}))
}
func newLogger(production bool) (l *zap.Logger) {
if production {
l, _ = zap.NewProduction()
} else {
l, _ = zap.NewDevelopment()
}
return
}
// NewLogger a wrap to newLogger
func NewLogger(production bool) *zap.Logger {
return newLogger(production)
}