-
Notifications
You must be signed in to change notification settings - Fork 3
/
logger.go
128 lines (108 loc) · 2.31 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
package logger
import (
"fmt"
"log"
"os"
"strings"
)
var (
level int
traceLogger = log.New(os.Stdout, "[T] ", log.Ldate|log.Ltime|log.Lshortfile)
debugLogger = log.New(os.Stdout, "[D] ", log.Ldate|log.Ltime|log.Lshortfile)
infoLogger = log.New(os.Stdout, "[I] ", log.Ldate|log.Ltime|log.Lshortfile)
warnLogger = log.New(os.Stdout, "[W] ", log.Ldate|log.Ltime|log.Lshortfile)
errorLogger = log.New(os.Stdout, "[E] ", log.Ldate|log.Ltime|log.Lshortfile)
fatalLogger = log.New(os.Stdout, "[F] ", log.Ldate|log.Ltime|log.Lshortfile)
)
func SetLevelWithDefault(lv, defaultLv string) {
err := SetLevel(lv)
if err != nil {
SetLevel(defaultLv)
}
}
func SetLevel(lv string) error {
if lv == "" {
return fmt.Errorf("log level is blank")
}
l := strings.ToUpper(lv)
switch l[0] {
case 'T':
level = 0
case 'D':
level = 1
case 'I':
level = 2
case 'W':
level = 3
case 'E':
level = 4
case 'F':
level = 5
default:
level = 6
}
if level == 6 {
return fmt.Errorf("log level setting error")
}
return nil
}
func Trace(format string, v ...interface{}) {
if 0 >= level {
traceLogger.Output(2, fmt.Sprintf(format, v...))
}
}
func Debug(format string, v ...interface{}) {
if 1 >= level {
debugLogger.Output(2, fmt.Sprintf(format, v...))
}
}
func Info(format string, v ...interface{}) {
if 2 >= level {
infoLogger.Output(2, fmt.Sprintf(format, v...))
}
}
func Warn(format string, v ...interface{}) {
if 3 >= level {
warnLogger.Output(2, fmt.Sprintf(format, v...))
}
}
func Error(format string, v ...interface{}) {
if 4 >= level {
errorLogger.Output(2, fmt.Sprintf(format, v...))
}
}
func Fatal(format string, v ...interface{}) {
if 5 >= level {
fatalLogger.Output(2, fmt.Sprintf(format, v...))
}
}
func Traceln(v ...interface{}) {
if 0 >= level {
traceLogger.Output(2, fmt.Sprintln(v...))
}
}
func Debugln(v ...interface{}) {
if 1 >= level {
debugLogger.Output(2, fmt.Sprintln(v...))
}
}
func Infoln(v ...interface{}) {
if 2 >= level {
infoLogger.Output(2, fmt.Sprintln(v...))
}
}
func Warnln(v ...interface{}) {
if 3 >= level {
warnLogger.Output(2, fmt.Sprintln(v...))
}
}
func Errorln(v ...interface{}) {
if 4 >= level {
errorLogger.Output(2, fmt.Sprintln(v...))
}
}
func Fatalln(v ...interface{}) {
if 5 >= level {
fatalLogger.Output(2, fmt.Sprintln(v...))
}
}