-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
144 lines (112 loc) · 2.62 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package hqgolog
import (
"os"
"strings"
"github.com/hueristiq/hqgolog/formatter"
"github.com/hueristiq/hqgolog/levels"
"github.com/hueristiq/hqgolog/writer"
)
type Logger struct { //nolint:govet // To be refactored.
formatter formatter.Formatter
maxLevel levels.LevelInt
writer writer.Writer
}
func (logger *Logger) SetFormatter(f formatter.Formatter) {
logger.formatter = f
}
func (logger *Logger) SetMaxLevel(level levels.LevelStr) {
logger.maxLevel = levels.Levels[level]
}
func (logger *Logger) SetWriter(w writer.Writer) {
logger.writer = w
}
func (logger *Logger) Log(event *Event) {
if event.level > logger.maxLevel {
return
}
var (
ok bool
label string
)
if _, ok = event.metadata["label"]; !ok {
labels := map[levels.LevelInt]string{
levels.Levels[levels.LevelFatal]: "FTL",
levels.Levels[levels.LevelError]: "ERR",
levels.Levels[levels.LevelWarn]: "WRN",
levels.Levels[levels.LevelInfo]: "INF",
levels.Levels[levels.LevelDebug]: "DBG",
}
if label, ok = labels[event.level]; ok {
event.metadata["label"] = label
}
}
event.message = strings.TrimSuffix(event.message, "\n")
data, err := logger.formatter.Format(&formatter.Log{
Message: event.message,
Level: event.level,
Metadata: event.metadata,
})
if err != nil {
return
}
if character, ok := event.metadata["rest"]; ok {
data = appendRest(data, character)
}
logger.writer.Write(data, event.level)
if event.level == levels.Levels[levels.LevelFatal] {
os.Exit(1)
}
}
func (logger *Logger) Print() *Event {
event := &Event{
logger: logger,
level: levels.LevelInt(-1),
metadata: make(map[string]string),
}
return event
}
func (logger *Logger) Debug() *Event {
level := levels.Levels[levels.LevelDebug]
event := &Event{
logger: logger,
level: level,
metadata: make(map[string]string),
}
return event
}
func (logger *Logger) Info() *Event {
level := levels.Levels[levels.LevelInfo]
event := &Event{
logger: logger,
level: level,
metadata: make(map[string]string),
}
return event
}
func (logger *Logger) Warn() *Event {
level := levels.Levels[levels.LevelWarn]
event := &Event{
logger: logger,
level: level,
metadata: make(map[string]string),
}
return event
}
func (logger *Logger) Error() *Event {
level := levels.Levels[levels.LevelError]
event := &Event{
logger: logger,
level: level,
metadata: make(map[string]string),
}
return event
}
func (logger *Logger) Fatal() *Event {
level := levels.Levels[levels.LevelFatal]
event := &Event{
logger: logger,
level: level,
metadata: make(map[string]string),
}
return event
}