-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
209 lines (181 loc) · 5.18 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package logger
import (
"fmt"
"io"
"log"
"os"
"runtime"
"sync"
"time"
)
type severity int
// Severity levels.
const (
sInfo severity = iota
sWarning
sError
sFatal
)
// Severity tags.
const (
tagInfo = "INFO : "
tagWarning = "WARN : "
tagError = "ERROR: "
tagFatal = "FATAL: "
)
//Log format flags
const (
LDate = 1 << iota // the date in the local time zone: 2009/01/23
LTime // the time in the local time zone: 01:23:23
LMicroseconds // microsecond resolution: 01:23:23.123123. assumes LTime.
LLongFile // full file name and line number: /a/b/c/d.go:23
LShortFile // final file name element and line number: d.go:23. overrides Llongfile
LUTC // if Ldate or Ltime is set, use UTC rather than the local time zone
LStdFlags = LDate | LTime | log.Lshortfile // initial values for the standard logger
)
var (
logLock sync.Mutex
defaultLogFile io.Writer
flag int
)
// set default log file to stdout when moule init
func init() {
logLock.Lock()
defer logLock.Unlock()
defaultLogFile = os.Stdout
flag = LStdFlags
}
// Init sets up output to non default and should be called before log functions, usually in
// the caller's main(). Default log output is stdout
// If the logFile passed in also satisfies io.Closer, logFile.Close will be called
// when closing the logger.
func Init(logFile io.Writer, flags int) {
logLock.Lock()
defer logLock.Unlock()
defaultLogFile = logFile
flag = flags
}
// Close closes the default logger.
func Close() {
logLock.Lock()
defer logLock.Unlock()
if defaultLogFile == nil {
return
}
if c, ok := defaultLogFile.(io.Closer); ok && c != nil {
_ = c.Close()
}
}
// Info uses the default logger and logs with the Info severity.
// Arguments are handled in the manner of fmt.Print.
func Info(v ...interface{}) {
output(sInfo, 2, fmt.Sprint(v...))
}
// Infof uses the default logger and logs with the Info severity.
// Arguments are handled in the manner of fmt.Printf.
func Infof(format string, v ...interface{}) {
output(sInfo, 2, fmt.Sprintf(format, v...))
}
// Warning uses the default logger and logs with the Warning severity.
// Arguments are handled in the manner of fmt.Print.
func Warning(v ...interface{}) {
output(sWarning, 2, fmt.Sprint(v...))
}
// Warningf uses the default logger and logs with the Warning severity.
// Arguments are handled in the manner of fmt.Printf.
func Warningf(format string, v ...interface{}) {
output(sWarning, 2, fmt.Sprintf(format, v...))
}
// Error uses the default logger and logs with the Error severity.
// Arguments are handled in the manner of fmt.Print.
func Error(v ...interface{}) {
output(sError, 2, fmt.Sprint(v...))
}
// Errorf uses the default logger and logs with the Error severity.
// Arguments are handled in the manner of fmt.Printf.
func Errorf(format string, v ...interface{}) {
output(sError, 2, fmt.Sprintf(format, v...))
}
// Fatal uses the default logger, logs with the Fatal severity,
// and ends with os.Exit(1).
// Arguments are handled in the manner of fmt.Print.
func Fatal(v ...interface{}) {
output(sFatal, 2, fmt.Sprint(v...))
Close()
os.Exit(1)
}
// Fatalf uses the default logger, logs with the Fatal severity,
// and ends with os.Exit(1).
// Arguments are handled in the manner of fmt.Printf.
func Fatalf(format string, v ...interface{}) {
output(sFatal, 2, fmt.Sprintf(format, v...))
Close()
os.Exit(1)
}
func output(level severity, callDepth int, message string) {
now := time.Now() // get this early.
var file string
var line int
logLock.Lock()
defer logLock.Unlock()
if flag&(LShortFile|LLongFile) != 0 {
// Release lock while getting caller info - it's expensive.
logLock.Unlock()
var ok bool
_, file, line, ok = runtime.Caller(callDepth)
if !ok {
file = "???"
line = 0
}
logLock.Lock()
}
buf := formatHeader(message, level, now, file, line)
if len(buf) == 0 || buf[len(buf)-1] != '\n' {
buf = append(buf, '\n')
}
_, _ = defaultLogFile.Write(buf)
}
func formatHeader(message string, level severity, t time.Time, file string, line int) []byte {
buf := make([]byte, 0)
if flag&(LDate|LTime|LMicroseconds) != 0 {
if flag&LUTC != 0 {
t = t.UTC()
}
if flag&LDate != 0 {
buf = append(buf, []byte(t.Format("2006/01/02"))...)
}
if flag&(LTime|LMicroseconds) != 0 {
buf = append(buf, []byte(t.Format(" 15:04:05"))...)
if flag&LMicroseconds != 0 {
buf = append(buf, []byte(t.Format(" .000000"))...)
}
buf = append(buf, ' ')
}
}
if flag&(LShortFile|LLongFile) != 0 {
if flag&LShortFile != 0 {
short := file
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
short = file[i+1:]
break
}
}
file = short
}
buf = append(buf, fmt.Sprintf("%s:%d ", file, line)...)
}
switch level {
case sInfo:
buf = append(buf, tagInfo...)
case sWarning:
buf = append(buf, tagWarning...)
case sError:
buf = append(buf, tagError...)
case sFatal:
buf = append(buf, tagFatal...)
default:
panic(fmt.Sprintln("unrecognized severity:", level))
}
return append(buf, message...)
}