/
levlog.go
200 lines (164 loc) · 5.04 KB
/
levlog.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
package levlog
import (
"fmt"
// "io"
"io/ioutil"
"log"
"os"
"runtime"
)
func init() {
Start(LevelTrace)
}
const (
LevelFatal = 1 // Highest level: important stuff down
LevelError = 2 // For example application crashes / exceptions.
LevelWarn = 3 // Incorrect behavior but the application can continue
LevelInfo = 4 // Normal behavior like mail sent, user updated profile etc.
LevelDebug = 5 // Executed queries, user authenticated, session expired
LevelTrace = 6 // Begin method X, end method X etc
)
type LevelLogger struct {
Trace *log.Logger
Debug *log.Logger
INFO *log.Logger
Warn *log.Logger
Error *log.Logger
Fatal *log.Logger
}
var logger LevelLogger
func init() {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
}
// Set log level. Can change during runtime
func Start(level int) int {
if level > LevelTrace {
level = LevelTrace
}
if level < LevelFatal {
level = LevelFatal
}
traceHandle := ioutil.Discard
debugHandle := ioutil.Discard
infoHandle := ioutil.Discard
warnHandle := ioutil.Discard
errorHandle := os.Stderr
fatalHandle := os.Stderr
switch level {
case LevelTrace:
traceHandle = os.Stdout
debugHandle = os.Stdout
infoHandle = os.Stdout
warnHandle = os.Stdout
case LevelDebug:
debugHandle = os.Stdout
infoHandle = os.Stdout
warnHandle = os.Stdout
case LevelInfo:
infoHandle = os.Stdout
warnHandle = os.Stdout
case LevelWarn:
warnHandle = os.Stdout
default: // always on
// case LevelError:
// case LevelFatal:
}
logger.Trace = log.New(traceHandle, "[TRACE] ", log.Ldate|log.Ltime|log.Lshortfile)
logger.Debug = log.New(debugHandle, "[DEBUG] ", log.Ldate|log.Ltime|log.Lshortfile)
logger.INFO = log.New(infoHandle, "[INFO ] ", log.Ldate|log.Ltime|log.Lshortfile)
logger.Warn = log.New(warnHandle, "[WARN ] ", log.Ldate|log.Ltime|log.Lshortfile)
logger.Error = log.New(errorHandle, "[ERROR] ", log.Ldate|log.Ltime|log.Lshortfile)
logger.Fatal = log.New(fatalHandle, "[FATAL] ", log.Ldate|log.Ltime|log.Lshortfile)
return level
}
//Most detail in printing.
func Trace(a ...interface{}) {
logger.Trace.Output(2, fmt.Sprintf("%s\n", fmt.Sprint(a...)))
}
func Tracef(format string, a ...interface{}) {
logger.Trace.Output(2, fmt.Sprintf("%s\n", fmt.Sprintf(format, a...)))
}
//For troubleshooting but not too much printout.
func Debug(a ...interface{}) {
logger.Debug.Output(2, fmt.Sprintf("%s\n", fmt.Sprint(a...)))
}
func Debugf(format string, a ...interface{}) {
logger.Debug.Output(2, fmt.Sprintf("%s\n", fmt.Sprintf(format, a...)))
}
func Info(a ...interface{}) {
logger.INFO.Output(2, fmt.Sprintf("%s\n", fmt.Sprint(a...)))
}
func Infof(format string, a ...interface{}) {
logger.INFO.Output(2, fmt.Sprintf("%s\n", fmt.Sprintf(format, a...)))
}
func Warning(a ...interface{}) {
logger.Warn.Output(2, fmt.Sprintf("%s\n", fmt.Sprint(a...)))
}
func Warningf(format string, a ...interface{}) {
logger.Warn.Output(2, fmt.Sprintf("%s\n", fmt.Sprintf(format, a...)))
}
func E(err error) {
if err != nil {
logger.Error.Output(2, err.Error())
}
}
func Error(a ...interface{}) {
logger.Error.Output(2, fmt.Sprintf("%s\n", fmt.Sprint(a...)))
}
func Errorf(format string, a ...interface{}) {
logger.Error.Output(2, fmt.Sprintf("%s\n", fmt.Sprintf(format, a...)))
}
//Severe problem.
func Fatal(a ...interface{}) {
logger.Fatal.Output(2, fmt.Sprintf("%s\n", fmt.Sprint(a...)))
os.Exit(1)
}
func F(err error) {
if err == nil{
return
}
logger.Fatal.Output(2, fmt.Sprintf("%s\n", fmt.Sprint(err.Error())))
os.Exit(1)
}
func Fatalf(format string, a ...interface{}) {
logger.Fatal.Output(2, fmt.Sprintf("%s\n", fmt.Sprintf(format, a...)))
os.Exit(1)
}
//Function start. Package and function will be output.
func Started() {
logger.Trace.Output(2, fmt.Sprintf("[%s] Started.\n", getCaller()))
}
//Function return. Package and function will be output.
func Completed() {
logger.Trace.Output(2, fmt.Sprintf("[%s] Completed.\n", getCaller()))
}
//Program exit with error. Package and function will be output.
func Exit(a ...interface{}) {
logger.Error.Output(2, fmt.Sprintf("[%s] Exit error: %s\n", getCaller(), fmt.Sprint(a...)))
}
func Exitf(format string, a ...interface{}) {
logger.Error.Output(2, fmt.Sprintf("[%s] Exit error: %s\n", getCaller(), fmt.Sprintf(format, a...)))
}
//Print file and line, package and function. Leave a foot print.
func Mark(a ...interface{}) {
logger.Trace.Output(2, fmt.Sprintf("[%s] %s\n", getCaller(), fmt.Sprint(a...)))
}
func Markf(format string, a ...interface{}) {
logger.Trace.Output(2, fmt.Sprintf("[%s] %s\n", getCaller(), fmt.Sprintf(format, a...)))
}
func getCaller() string {
pc := make([]uintptr, 10) // at least 1 entry needed
runtime.Callers(3, pc)
f := runtime.FuncForPC(pc[0])
return f.Name()
}
//To keep backward compatible with existing log printout.
func Print(v ...interface{}) {
logger.INFO.Output(2, fmt.Sprintf("%s", fmt.Sprint(v...)))
}
func Println(v ...interface{}) {
logger.INFO.Output(2, fmt.Sprintf("%s\n", fmt.Sprint(v...)))
}
func Printf(format string, v ...interface{}) {
logger.INFO.Output(2, fmt.Sprintf("%s\n", fmt.Sprintf(format, v...)))
}