-
Notifications
You must be signed in to change notification settings - Fork 0
/
golog.go
213 lines (177 loc) · 5.62 KB
/
golog.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
210
211
212
213
package golog
import (
"fmt"
"os"
"path"
"runtime"
"time"
)
type Level int
const (
LOG_PLAIN Level = iota
LOG_DEBUG
LOG_INFO
LOG_ERROR
LOG_FATAL
LOG_PANIC
)
var (
LogLevel = LOG_INFO
DateFormat = "2006-01-02 15:04:05.000"
FormatFunctions = map[Level]func(*os.File, string, string, int, string, string){
LOG_PLAIN: LogPlain,
LOG_DEBUG: LogDefault,
LOG_INFO: LogDefault,
LOG_ERROR: LogDefault,
LOG_FATAL: LogDefault,
LOG_PANIC: LogDefault,
}
// The current maximum length printed for caller information. This is updated each time something gets printed
CallerColumnWidth = 0
LevelStrings = map[Level]string{
LOG_PLAIN: "",
LOG_DEBUG: "[DEBUG]",
LOG_INFO: "[INFO] ",
LOG_ERROR: "[ERROR]",
LOG_FATAL: "[FATAL]",
LOG_PANIC: "[PANIC]",
}
LevelOutputs = map[Level]*os.File{
LOG_PLAIN: os.Stdout,
LOG_DEBUG: os.Stdout,
LOG_INFO: os.Stdout,
LOG_ERROR: os.Stderr,
LOG_FATAL: os.Stderr,
LOG_PANIC: os.Stderr,
}
)
func Plain(format string, a ...interface{}) {
if LogLevel <= LOG_PLAIN {
log(LOG_PLAIN, 3, fmt.Sprintf(format, a...))
}
}
// Plainb is equal to Plain(...) but can go back in the stack and can therefore show function positions from previous functions.
func Plainb(framesBackward int, format string, a ...interface{}) {
if LogLevel <= LOG_PLAIN {
log(LOG_PLAIN, 3+framesBackward, fmt.Sprintf(format, a...))
}
}
func Info(format string, a ...interface{}) {
if LogLevel <= LOG_INFO {
log(LOG_INFO, 3, fmt.Sprintf(format, a...))
}
}
// Infob is equal to Info(...) but can go back in the stack and can therefore show function positions from previous functions.
func Infob(framesBackward int, format string, a ...interface{}) {
if LogLevel <= LOG_INFO {
log(LOG_INFO, 3+framesBackward, fmt.Sprintf(format, a...))
}
}
func Debug(format string, a ...interface{}) {
if LogLevel <= LOG_DEBUG {
log(LOG_DEBUG, 3, fmt.Sprintf(format, a...))
}
}
// Debugb is equal to Debug(...) but can go back in the stack and can therefore show function positions from previous functions.
func Debugb(framesBackward int, format string, a ...interface{}) {
if LogLevel <= LOG_DEBUG {
log(LOG_DEBUG, 3+framesBackward, fmt.Sprintf(format, a...))
}
}
func Error(format string, a ...interface{}) {
if LogLevel <= LOG_ERROR {
log(LOG_ERROR, 3, fmt.Sprintf(format, a...))
}
}
// Errorb is equal to Error(...) but can go back in the stack and can therefore show function positions from previous functions.
func Errorb(framesBackward int, format string, a ...interface{}) {
if LogLevel <= LOG_ERROR {
log(LOG_ERROR, 3+framesBackward, fmt.Sprintf(format, a...))
}
}
func Panic(format string, a ...interface{}) {
s := fmt.Sprintf(format, a...)
log(LOG_PANIC, 3, s)
panic(s)
}
// Stack tries to print the stack trace of the given error using the %+v format string.
// When using the https://github.com/pkg/errors package, a full error stack trace will be output.
// If normal errors are used, just print the error.
func Stack(err error) {
if LogLevel <= LOG_ERROR {
// Directly call "log" to avoid extra function call
log(LOG_ERROR, 3, fmt.Sprintf("%+v", err))
}
}
// Stackb is equal to Stack(...) but can go back in the stack and can therefore show function positions from previous functions.
func Stackb(framesBackward int, err error) {
if LogLevel <= LOG_ERROR {
// Directly call "log" to avoid extra function call
log(LOG_ERROR, 3+framesBackward, fmt.Sprintf("%+v", err))
}
}
func Fatal(format string, a ...interface{}) {
log(LOG_FATAL, 3, fmt.Sprintf(format, a...))
os.Exit(1)
}
// FatalCheckf checks if the error exists.
// If so, it'll print the error message and fatals with the given format message.
func FatalCheckf(err error, format string, a ...interface{}) {
if err != nil {
Stackb(1, err)
if a != nil {
internalFatal(format, a...)
} else {
internalFatal(format)
}
}
}
// FatalCheck checks if the error exists (!= nil).
// If so, it'll fatal with the error message.
func FatalCheck(err error) {
if err != nil {
Stackb(1, err)
os.Exit(1)
}
}
func internalFatal(format string, a ...interface{}) {
internalLog(LOG_FATAL, fmt.Sprintf(format, a...))
os.Exit(1)
}
func log(level Level, framesBackward int, message string) {
// We know here that the stack contains two calls from inside this file.
// The third frame comes from the file that initially called a function
// in this file e.g. Info()
caller := getCallerDetails(framesBackward)
updateCallerColumnWidth(caller)
FormatFunctions[level](LevelOutputs[level], time.Now().Format(DateFormat), LevelStrings[level], CallerColumnWidth, caller, message)
}
func internalLog(level Level, message string) {
// We know here that the stack contains three calls from inside this file.
// The third frame comes from the file that initially called a function
// in this file e.g. Info()
caller := getCallerDetails(4)
updateCallerColumnWidth(caller)
FormatFunctions[level](LevelOutputs[level], time.Now().Format(DateFormat), LevelStrings[level], CallerColumnWidth, caller, message)
}
func getCallerDetails(framesBackwards int) string {
name := ""
line := -1
ok := false
if _, name, line, ok = runtime.Caller(framesBackwards); ok {
name = path.Base(name)
}
caller := fmt.Sprintf("%s:%d", name, line)
return caller
}
func updateCallerColumnWidth(caller string) {
if len(caller) > CallerColumnWidth {
CallerColumnWidth = len(caller)
}
}
func LogPlain(writer *os.File, time, level string, maxLength int, caller, message string) {
fmt.Fprintf(writer, "%s\n", message)
}
func LogDefault(writer *os.File, time, level string, maxLength int, caller, message string) {
fmt.Fprintf(writer, "%s %s %-*s | %s\n", time, level, maxLength, caller, message)
}