-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
151 lines (127 loc) · 2.97 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package logger
import (
"fmt"
"io"
"log"
)
type Logger struct {
level LogLevel
flags int
error *log.Logger
info *log.Logger
warn *log.Logger
debug *log.Logger
}
// Create a new custom Logger
func New(writer io.Writer) Logger {
return Logger{
level: defaultLogLevel,
error: log.New(writer, "[ERROR] ", defaultLogFlags),
info: log.New(writer, "[INFO ] ", defaultLogFlags),
warn: log.New(writer, "[WARN ] ", defaultLogFlags),
debug: log.New(writer, "[DEBUG] ", defaultLogFlags),
}
}
func (l *Logger) Flags() int {
return l.flags
}
func (l *Logger) Level() LogLevel {
return l.level
}
func (l *Logger) SetLevel(level LogLevel) {
l.level = level
}
func (l *Logger) SetOutput(w io.Writer) {
l.error.SetOutput(w)
l.info.SetOutput(w)
l.warn.SetOutput(w)
l.debug.SetOutput(w)
}
func (l *Logger) SetFlags(flags int) {
l.flags = flags
l.error.SetFlags(flags)
l.info.SetFlags(flags)
l.warn.SetFlags(flags)
l.debug.SetFlags(flags)
}
func (l *Logger) Debug(v ...interface{}) {
if l.level >= LvDebug {
l.debug.Output(2, fmt.Sprint(v...))
}
}
func (l *Logger) Info(v ...interface{}) {
if l.level >= LvInfo {
l.info.Output(2, fmt.Sprint(v...))
}
}
func (l *Logger) Warn(v ...interface{}) {
if l.level >= LvWarn {
l.warn.Output(2, fmt.Sprint(v...))
}
}
func (l *Logger) Error(v ...interface{}) {
if l.level >= LvError {
l.error.Output(2, fmt.Sprint(v...))
}
}
func (l *Logger) Debugf(format string, v ...interface{}) {
if l.level >= LvDebug {
l.debug.Output(2, fmt.Sprintf(format, v...))
}
}
func (l *Logger) Infof(format string, v ...interface{}) {
if l.level >= LvInfo {
l.info.Output(2, fmt.Sprintf(format, v...))
}
}
func (l *Logger) Warnf(format string, v ...interface{}) {
if l.level >= LvWarn {
l.warn.Output(2, fmt.Sprintf(format, v...))
}
}
func (l *Logger) Errorf(format string, v ...interface{}) {
if l.level >= LvError {
l.error.Output(2, fmt.Sprintf(format, v...))
}
}
// Following methods omit caller's call-stack when logging
func (l *Logger) callerDebug(v ...interface{}) {
if l.level >= LvDebug {
l.debug.Output(3, fmt.Sprint(v...))
}
}
func (l *Logger) callerInfo(v ...interface{}) {
if l.level >= LvInfo {
l.info.Output(3, fmt.Sprint(v...))
}
}
func (l *Logger) callerWarn(v ...interface{}) {
if l.level >= LvWarn {
l.warn.Output(3, fmt.Sprint(v...))
}
}
func (l *Logger) callerError(v ...interface{}) {
if l.level >= LvError {
l.error.Output(3, fmt.Sprint(v...))
}
}
func (l *Logger) callerDebugf(format string, v ...interface{}) {
if l.level >= LvDebug {
l.debug.Output(3, fmt.Sprintf(format, v...))
}
}
func (l *Logger) callerInfof(format string, v ...interface{}) {
if l.level >= LvInfo {
l.info.Output(3, fmt.Sprintf(format, v...))
}
}
func (l *Logger) callerWarnf(format string, v ...interface{}) {
if l.level >= LvWarn {
l.warn.Output(3, fmt.Sprintf(format, v...))
}
}
func (l *Logger) callerErrorf(format string, v ...interface{}) {
if l.level >= LvError {
l.error.Output(3, fmt.Sprintf(format, v...))
}
}