forked from go-spatial/tegola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
188 lines (166 loc) · 2.86 KB
/
log.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package log
import (
"fmt"
"io"
"sync"
)
var TimestampRegex string = `\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}` // Ex: "2006-01-02 15:04:05"
type Interface interface {
// These all take args the same as calls to fmt.Printf()
Fatal(...interface{})
Error(...interface{})
Warn(...interface{})
Info(...interface{})
Debug(...interface{})
Trace(...interface{})
SetOutput(io.Writer)
}
func init() {
logger = standard
SetLogLevel(INFO)
}
type Level int
// Allows the ordering of severity to be checked
const (
TRACE = Level(iota - 2)
DEBUG
INFO
WARN
ERROR
FATAL
)
var (
logger Interface
level Level
lock sync.Mutex
// FATAL level is never disabled
IsError bool
IsWarn bool
IsInfo bool
IsDebug bool
IsTrace bool
)
func (lvl Level) String() string {
switch lvl {
case TRACE:
return "TRACE"
case DEBUG:
return "DEBUG"
case INFO:
return "INFO"
case WARN:
return "WARN"
case ERROR:
return "ERROR"
case FATAL:
return "FATAL"
default:
return "UNKNOWN LEVEL"
}
}
func SetOutput(w io.Writer) {
logger.SetOutput(w)
}
func SetLogLevel(lvl Level) {
lock.Lock()
IsTrace = false
IsDebug = false
IsInfo = false
IsWarn = false
IsError = false
IsTrace = false
if lvl != TRACE && lvl != DEBUG && lvl != INFO && lvl != WARN && lvl != ERROR && lvl != FATAL {
lvl = INFO
}
level = lvl
switch level {
case TRACE:
IsTrace = true
fallthrough
case DEBUG:
IsDebug = true
fallthrough
case INFO:
IsInfo = true
fallthrough
case WARN:
IsWarn = true
fallthrough
case ERROR:
IsError = true
}
lock.Unlock()
}
// Output format should be: "timestamp•LOG_LEVEL•filename.go•linenumber•output"
func Fatalf(format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
logger.Fatal(msg)
}
func Errorf(format string, args ...interface{}) {
if !IsError {
return
}
msg := fmt.Sprintf(format, args...)
logger.Error(msg)
}
func Warnf(format string, args ...interface{}) {
if !IsWarn {
return
}
msg := fmt.Sprintf(format, args...)
logger.Warn(msg)
}
func Infof(format string, args ...interface{}) {
if !IsInfo {
return
}
msg := fmt.Sprintf(format, args...)
logger.Info(msg)
}
func Debugf(format string, args ...interface{}) {
if !IsDebug {
return
}
msg := fmt.Sprintf(format, args...)
logger.Debug(msg)
}
func Tracef(format string, args ...interface{}) {
if !IsTrace {
return
}
msg := fmt.Sprintf(format, args...)
logger.Trace(msg)
}
func Fatal(args ...interface{}) {
logger.Fatal(args...)
}
func Error(args ...interface{}) {
if !IsError {
return
}
logger.Error(args...)
}
func Warn(args ...interface{}) {
if !IsWarn {
return
}
logger.Warn(args...)
}
func Info(args ...interface{}) {
if !IsInfo {
return
}
logger.Info(args...)
}
func Debug(args ...interface{}) {
if !IsDebug {
return
}
logger.Debug(args...)
}
func Trace(args ...interface{}) {
if !IsTrace {
return
}
logger.Trace(args...)
}