-
Notifications
You must be signed in to change notification settings - Fork 6
/
logger.go
120 lines (101 loc) · 2.87 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package configcat
import (
"fmt"
"log"
"os"
"strconv"
)
const (
LogLevelDebug LogLevel = -2
LogLevelInfo LogLevel = -1
LogLevelWarn LogLevel = 0
LogLevelError LogLevel = 1
LogLevelNone LogLevel = 2
)
type LogLevel int
// Logger defines the interface this library logs with.
type Logger interface {
Debugf(format string, args ...interface{})
Infof(format string, args ...interface{})
Warnf(format string, args ...interface{})
Errorf(format string, args ...interface{})
}
// DefaultLogger creates the default logger with specified log level.
func DefaultLogger() Logger {
return &defaultLogger{Logger: log.New(os.Stderr, "[ConfigCat] ", log.LstdFlags)}
}
func newLeveledLogger(logger Logger, level LogLevel, hooks *Hooks) *leveledLogger {
if logger == nil {
logger = DefaultLogger()
}
return &leveledLogger{
minLevel: level,
Logger: logger,
hooks: hooks,
}
}
// leveledLogger wraps a Logger for efficiency reasons: it's a static type
// rather than an interface so the compiler can inline the level check
// and thus avoid the allocation for the arguments.
type leveledLogger struct {
minLevel LogLevel
hooks *Hooks
Logger
}
type defaultLogger struct {
*log.Logger
}
func (log *leveledLogger) enabled(level LogLevel) bool {
return level >= log.minLevel
}
func (log *leveledLogger) Debugf(format string, args ...interface{}) {
if log.enabled(LogLevelDebug) {
log.Logger.Debugf("[0] "+format, args...)
}
}
func (log *leveledLogger) Infof(eventId int, format string, args ...interface{}) {
if log.enabled(LogLevelInfo) {
log.Logger.Infof("["+strconv.Itoa(eventId)+"] "+format, args...)
}
}
func (log *leveledLogger) Warnf(eventId int, format string, args ...interface{}) {
if log.enabled(LogLevelWarn) {
log.Logger.Warnf("["+strconv.Itoa(eventId)+"] "+format, args...)
}
}
func (log *leveledLogger) Errorf(eventId int, format string, args ...interface{}) {
if log.hooks != nil && log.hooks.OnError != nil {
go log.hooks.OnError(fmt.Errorf(format, args...))
}
if log.enabled(LogLevelError) {
log.Logger.Errorf("["+strconv.Itoa(eventId)+"] "+format, args...)
}
}
func (l *defaultLogger) Debugf(format string, args ...interface{}) {
l.logf(LogLevelDebug, format, args...)
}
func (l *defaultLogger) Infof(format string, args ...interface{}) {
l.logf(LogLevelInfo, format, args...)
}
func (l *defaultLogger) Warnf(format string, args ...interface{}) {
l.logf(LogLevelWarn, format, args...)
}
func (l *defaultLogger) Errorf(format string, args ...interface{}) {
l.logf(LogLevelError, format, args...)
}
func (l *defaultLogger) logf(level LogLevel, format string, args ...interface{}) {
l.Logger.Printf(level.String()+": "+format, args...)
}
func (lvl LogLevel) String() string {
switch lvl {
case LogLevelDebug:
return "DEBUG"
case LogLevelInfo:
return "INFO"
case LogLevelWarn:
return "WARN"
case LogLevelError:
return "ERROR"
}
return "UNKNOWN"
}