-
Notifications
You must be signed in to change notification settings - Fork 1
/
u_logger.go
78 lines (65 loc) · 1.19 KB
/
u_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
package kerbalwzygo
import (
"fmt"
"log"
"os"
"sync"
)
type Level int
var (
Debug Level = 0
Info Level = 1
Warn Level = 2
Error Level = 3
)
type XLogger struct {
level Level
log.Logger
}
func (obj *XLogger) SetLevel(level Level) {
obj.level = level
}
func (obj *XLogger) Level() Level {
if obj.level == 0 {
obj.level = Debug
}
return obj.level
}
// Printf rewrite for correct 'calldepth' value
func (obj *XLogger) Printf(format string, v ...interface{}) {
_ = obj.Output(3, fmt.Sprintf(format, v...))
}
func (obj *XLogger) Debug(msg ...interface{}) {
if obj.level > Debug {
return
}
obj.Printf("[DEBUG] %s", msg)
}
func (obj *XLogger) Info(msg ...interface{}) {
if obj.level > Info {
return
}
obj.Printf("[INFO] %s", msg)
}
func (obj *XLogger) Warn(msg ...interface{}) {
if obj.level > Warn {
return
}
obj.Printf("[WARN] %s", msg)
}
func (obj *XLogger) Error(msg ...interface{}) {
if obj.level > Error {
return
}
obj.Printf("[ERROR] %s", msg)
}
var logger *XLogger
var once sync.Once
func GetLogger() *XLogger {
once.Do(func() {
logger = &XLogger{}
logger.SetOutput(os.Stdout)
logger.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
})
return logger
}