This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_impl.go
147 lines (118 loc) · 3.13 KB
/
logger_impl.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
145
146
147
package log
type logger struct {
level Level
labels Labels
writer Writer
}
func (pipeline *logger) Close() error {
return pipeline.writer.Close()
}
func (pipeline *logger) Rotate() error {
return pipeline.writer.Rotate()
}
func (pipeline *logger) WithLevel(level Level) Logger {
return &logger{
level: level,
labels: pipeline.labels,
writer: pipeline.writer,
}
}
func (pipeline *logger) WithLabel(labelName LabelName, labelValue LabelValue) Logger {
newLabels := make(Labels, len(pipeline.labels))
for k, v := range pipeline.labels {
newLabels[k] = v
}
newLabels[labelName] = labelValue
return &logger{
level: pipeline.level,
labels: newLabels,
writer: pipeline.writer,
}
}
//region Format
func (pipeline *logger) write(level Level, message ...interface{}) {
if pipeline.level >= level {
if len(message) == 0 {
return
}
var msg Message
if len(message) == 1 {
switch message[0].(type) {
case string:
msg = NewMessage(EUnknownError, message[0].(string))
default:
if m, ok := message[0].(Message); ok {
msg = m
} else if m, ok := message[0].(error); ok {
msg = pipeline.wrapError(m)
} else {
msg = NewMessage(EUnknownError, "%v", message[0])
}
}
} else {
msg = NewMessage(EUnknownError, "%v", message)
}
for label, value := range pipeline.labels {
msg = msg.Label(label, value)
}
if err := pipeline.writer.Write(level, msg); err != nil {
panic(err)
}
}
}
func (pipeline *logger) writef(level Level, format string, args ...interface{}) {
if pipeline.level >= level {
var msg Message
msg = NewMessage(EUnknownError, format, args...)
for label, value := range pipeline.labels {
msg = msg.Label(label, value)
}
if err := pipeline.writer.Write(level, msg); err != nil {
panic(err)
}
}
}
func (pipeline *logger) wrapError(err error) Message {
return Wrap(
err,
EUnknownError,
"An unexpected error has happened.",
)
}
//endregion
//region Messages
func (pipeline *logger) Emergency(message ...interface{}) {
pipeline.write(LevelEmergency, message...)
}
func (pipeline *logger) Alert(message ...interface{}) {
pipeline.write(LevelAlert, message...)
}
func (pipeline *logger) Critical(message ...interface{}) {
pipeline.write(LevelCritical, message...)
}
func (pipeline *logger) Error(message ...interface{}) {
pipeline.write(LevelError, message...)
}
func (pipeline *logger) Warning(message ...interface{}) {
pipeline.write(LevelWarning, message...)
}
func (pipeline *logger) Notice(message ...interface{}) {
pipeline.write(LevelNotice, message...)
}
func (pipeline *logger) Info(message ...interface{}) {
pipeline.write(LevelInfo, message...)
}
func (pipeline *logger) Debug(message ...interface{}) {
pipeline.write(LevelDebug, message...)
}
//endregion
//region Log
// Log provides a generic log method that logs on the info level.
func (pipeline *logger) Log(args ...interface{}) {
pipeline.writef(LevelInfo, "%v", args...)
}
// Logf provides a generic log method that logs on the info level with formatting.
func (pipeline *logger) Logf(format string, args ...interface{}) {
pipeline.writef(LevelInfo, format, args...)
}
//endregion