-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
102 lines (87 loc) · 1.75 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
package redlog
const LOG_FORMAT string = "%s | [%s] - %s\n"
//LogFlags container for flags
type LogFlags uint
type logChan chan logMessage
type errChan chan error
type logMessage struct {
level LogType
message string
}
//ILog logging interface
type ILog interface {
Level(level LogLevel)
Flags(flags LogFlags)
Debug(message string)
Info(message string)
Warning(message string)
Error(message string)
Critical(message string)
Close() error
}
//BaseLog base log
type BaseLog struct {
level LogLevel
flags LogFlags
io logChan
err errChan
}
func NewBaseLog(level LogLevel) BaseLog {
return BaseLog{
level: level,
io: make(logChan),
err: make(errChan),
}
}
func (log *BaseLog) Close() error {
if log.io != nil {
close(log.io)
if log.err != nil {
return <-log.err
}
}
return nil
}
//Level sets write level
func (log *BaseLog) Level(level LogLevel) {
log.level = level
}
//Flags sets write flags
func (log *BaseLog) Flags(flags LogFlags) {
log.flags = flags
}
//Debug generates a DEBUG level log
func (log *BaseLog) Debug(message string) {
log.io <- logMessage{
level: TYPE_DEBUG,
message: message,
}
}
//Info generates an INFO level log
func (log *BaseLog) Info(message string) {
log.io <- logMessage{
level: TYPE_INFO,
message: message,
}
}
//Warning generates a WARNING level log
func (log *BaseLog) Warning(message string) {
log.io <- logMessage{
level: TYPE_WARNING,
message: message,
}
}
//Error generates an ERROR level log
func (log *BaseLog) Error(message string) {
log.io <- logMessage{
level: TYPE_ERROR,
message: message,
}
}
//Critical generates a CRITICAL level log
func (log *BaseLog) Critical(message string) {
log.io <- logMessage{
level: TYPE_CRITICAL,
message: message,
}
}