-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlevel.go
78 lines (62 loc) · 1.53 KB
/
level.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
package asyncLog
import (
"math/rand"
"fmt"
)
// 日志优先级
type Priority int
const (
LevelAll Priority = iota
LevelDebug
LevelInfo
LevelWarn
LevelError
LevelFatal
LevelOff
)
var (
// 日志等级
levelTitle = map[Priority]string{
LevelDebug: "[DEBUG]",
LevelInfo: "[INFO]",
LevelWarn: "[WARN]",
LevelError: "[ERROR]",
LevelFatal: "[FATAL]",
}
)
// NewLevelLog 写入等级日志
// 级别高于logLevel才会被写入
func NewLevelLog(filename string, logLevel Priority) *LogFile {
lf := NewLogFile(filename)
lf.level = logLevel
return lf
}
func (lf *LogFile) SetLevel(logLevel Priority) {
lf.level = logLevel
}
func (lf *LogFile) Debug(format string, a ...interface{}) error {
return lf.writeLevelMsg(LevelDebug, format, a...)
}
func (lf *LogFile) Info(format string, a ...interface{}) error {
return lf.writeLevelMsg(LevelInfo, format, a...)
}
func (lf *LogFile) Warn(format string, a ...interface{}) error {
return lf.writeLevelMsg(LevelWarn, format, a...)
}
func (lf *LogFile) Error(format string, a ...interface{}) error {
return lf.writeLevelMsg(LevelError, format, a...)
}
func (lf *LogFile) Fatal(format string, a ...interface{}) error {
return lf.writeLevelMsg(LevelFatal, format, a...)
}
func (lf *LogFile) writeLevelMsg(level Priority, format string, a ...interface{}) error {
if lf.probability < 1.0 && rand.Float32() > lf.probability {
// 按照概率写入
return nil
}
if level >= lf.level {
msg := fmt.Sprintf(format, a...)
return lf.Write(levelTitle[level] + " " + msg)
}
return nil
}