-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.go
109 lines (91 loc) · 1.84 KB
/
log.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
package parse
import (
"fmt"
)
// LogLevel 日志级别
type LogLevel int
const (
LogDebug LogLevel = 0
LogInfo LogLevel = 1
LogWarning LogLevel = 2
LogError LogLevel = 3
)
var (
log ILog
logEnable bool
logLevel LogLevel
)
// ILog 日志模块
type ILog interface {
Debug(format string, a ...interface{})
Info(format string, a ...interface{})
Error(format string, a ...interface{})
Warning(format string, a ...interface{})
}
func init() {
SetLog(&defaultLog{})
}
// SetLog 设置日志模块
func SetLog(ilog ILog) {
log = ilog
logEnable = true
logLevel = LogDebug
}
// SetLogEnable 是否开启日志
func SetLogEnable(enable bool) {
logEnable = enable
}
// SetLogLevel 设置日志级别
func SetLogLevel(level LogLevel) {
logLevel = level
}
// Debug log
func Debug(format string, a ...interface{}) {
if log != nil &&
logEnable &&
LogDebug >= logLevel {
log.Debug(format, a...)
}
}
// Info log
func Info(format string, a ...interface{}) {
if log != nil &&
logEnable &&
LogInfo >= logLevel {
log.Info(format, a...)
}
}
// Error log
func Error(format string, a ...interface{}) {
if log != nil &&
logEnable &&
LogError >= logLevel {
log.Error(format, a...)
}
}
// Warning log
func Warning(format string, a ...interface{}) {
if log != nil &&
logEnable &&
LogWarning >= logLevel {
log.Warning(format, a...)
}
}
type defaultLog struct {
}
// Debug log
func (d *defaultLog) Debug(format string, a ...interface{}) {
fmt.Println(fmt.Sprintf(format, a...))
}
// Info log
func (d *defaultLog) Info(format string, a ...interface{}) {
fmt.Println(fmt.Sprintf(format, a...))
}
// Error log
func (d *defaultLog) Error(format string, a ...interface{}) {
fmt.Println(fmt.Sprintf(format, a...))
}
// Warning log
func (d *defaultLog) Warning(format string, a ...interface{}) {
fmt.Println(fmt.Sprintf(format, a...))
}