-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathlog.go
More file actions
57 lines (47 loc) · 969 Bytes
/
log.go
File metadata and controls
57 lines (47 loc) · 969 Bytes
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
package exapi
import (
"fmt"
"log"
)
// 日志接口,外部系统可以使用此接口来替换默认日志
type Logger interface {
Write(string)
}
// 默认日志接口
var defaultLogger Logger
// 日志开关,默认为打开状态
var logOn bool = true
// 设置自定义日志接口,只需要实现接口 Write(string)
func SetLogger(l Logger) {
defaultLogger = l
}
// 开启日志
func OpenLog() {
logOn = true
}
// 关闭日志
func CloseLog() {
logOn = false
}
// 获取日志状态
func IsLoging() bool {
return logOn
}
// 打印日志
func Log(format string, v ...interface{}) {
if logOn {
if defaultLogger != nil {
defaultLogger.Write(fmt.Sprintf(format+"\n", v...))
} else {
log.Printf(format+"\n", v...)
}
}
}
// 打印错误日志,不可关闭
func Error(format string, v ...interface{}) {
if defaultLogger != nil {
defaultLogger.Write(fmt.Sprintf(format+"\n", v...))
} else {
log.Printf(format+"\n", v...)
}
}