-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
109 lines (86 loc) · 2.24 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
package rpl
import "fmt"
// Log levels
const (
LevelSilent int8 = 0
LevelFatal int8 = 1
LevelError int8 = 1
LevelSuccess int8 = 1
LevelInfo int8 = 2
LevelWarn int8 = 2
LevelDebug int8 = 3
)
// Logger is the most common Source,
// produce Log on remote.
type Logger struct {
targets []Target
// [Ch] is the [Log] channel of [Logger].
Ch uint16
}
func NewLogger(ch uint16) *Logger {
return &Logger{
Ch: ch,
}
}
func (logger *Logger) Register(target Target) {
logger.targets = append(logger.targets, target)
}
func (logger *Logger) Close() {
for _, target := range logger.targets {
target.Close()
}
}
func (logger *Logger) Logs(level int8, value string) {
log := &Log{
Ch: logger.Ch,
Level: level,
Value: value,
}
for _, target := range logger.targets {
target.Writer() <- log
}
}
func (logger *Logger) Log(level int8, args ...interface{}) {
logger.Logs(level, fmt.Sprint(args...))
}
func (logger *Logger) Logf(level int8, format string, args ...interface{}) {
logger.Log(level, fmt.Sprintf(format, args...))
}
func (logger *Logger) Success(args ...interface{}) {
logger.Log(LevelSuccess, args...)
}
func (logger *Logger) Fatal(args ...interface{}) {
logger.Log(LevelFatal, args...)
logger.Close()
}
func (logger *Logger) Error(args ...interface{}) {
logger.Log(LevelError, args...)
}
func (logger *Logger) Info(args ...interface{}) {
logger.Log(LevelInfo, args...)
}
func (logger *Logger) Warn(args ...interface{}) {
logger.Log(LevelWarn, args...)
}
func (logger *Logger) Debug(args ...interface{}) {
logger.Log(LevelDebug, args...)
}
func (logger *Logger) Successf(format string, args ...interface{}) {
logger.Logf(LevelSuccess, format, args...)
}
func (logger *Logger) Fatalf(format string, args ...interface{}) {
logger.Logf(LevelFatal, format, args...)
logger.Close()
}
func (logger *Logger) Errorf(format string, args ...interface{}) {
logger.Logf(LevelError, format, args...)
}
func (logger *Logger) Infof(format string, args ...interface{}) {
logger.Logf(LevelInfo, format, args...)
}
func (logger *Logger) Warnf(format string, args ...interface{}) {
logger.Logf(LevelWarn, format, args...)
}
func (logger *Logger) Debugf(format string, args ...interface{}) {
logger.Logf(LevelDebug, format, args...)
}