forked from ttacon/glog
-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.go
201 lines (173 loc) · 6.22 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package glog
import (
"context"
"fmt"
)
// Logger provides logging functionality with additional data and prefixing.
type Logger struct {
*loggingT
prefix string
// Extra arguments to be appended to each request
data []interface{}
}
// NewLogger creates a Logger instance with no additional data.
func NewLogger() *Logger {
return &Logger{
loggingT: &logging,
}
}
// WithContext creates a logger from a context.Context
func WithContext(ctx context.Context) *Logger {
data, _ := ctx.Value(contextKeyData).([]interface{})
prefix, _ := ctx.Value(contextKeyPrefix).(string)
return &Logger{
loggingT: &logging,
data: data,
prefix: prefix,
}
}
// WithPrefix creates a Logger with a given prefix.
func WithPrefix(prefix string) *Logger {
return &Logger{
loggingT: &logging,
prefix: prefix,
}
}
// WithData creates a Logger with a given set of backend data.
// All args provided will be wrapped with glog.Data prior to being sent.
func WithData(args ...interface{}) *Logger {
return &Logger{
loggingT: &logging,
data: args,
}
}
// WithPrefix creates a Logger from an existing logger with a specified prefix.
// Any prefix on the input Logger will be replaced.
func (l *Logger) WithPrefix(prefix string) *Logger {
return &Logger{
loggingT: l.loggingT,
data: l.data,
prefix: prefix,
}
}
// WithData creates a Logger from an existing logger, using the specifed data.
// Any existing data on the input Logger will be replaced.
func (l *Logger) WithData(vars ...interface{}) *Logger {
return &Logger{
loggingT: l.loggingT,
data: vars,
prefix: l.prefix,
}
}
// AppendData creates a Logger from an existing logger,
// appending the provided data to the data in the existing logger.
func (l *Logger) AppendData(vars ...interface{}) *Logger {
newData := make([]interface{}, len(l.data))
copy(newData, l.data)
return &Logger{
loggingT: l.loggingT,
data: append(newData, vars...),
prefix: l.prefix,
}
}
// Info is equivalent to the global Info function, with the addition of prefix and data content from this Logger.
func (l *Logger) Info(args ...interface{}) {
l.print(infoLog, l.extendWithPfx(args)...)
}
// Infoln is equivalent to the global Infoln function, with the addition of prefix and data content from this Logger.
func (l *Logger) Infoln(args ...interface{}) {
l.println(infoLog, l.extendWithPfx(args)...)
}
// Infof is equivalent to the global Infof function, with the addition of prefix and data content from this Logger.
func (l *Logger) Infof(format string, args ...interface{}) {
l.printf(infoLog, l.pfx(format), l.extend(args)...)
}
// Warning is equivalent to the global Warning function, with the addition of prefix and data content from this Logger.
func (l *Logger) Warning(args ...interface{}) {
l.print(warningLog, l.extendWithPfx(args)...)
}
// Warningln is equivalent to the global Warningln function, with the addition of prefix and data content from this Logger.
func (l *Logger) Warningln(args ...interface{}) {
l.println(warningLog, l.extendWithPfx(args)...)
}
// Warningf is equivalent to the global Warningf function, with the addition of prefix and data content from this Logger.
func (l *Logger) Warningf(format string, args ...interface{}) {
l.printf(warningLog, l.pfx(format), l.extend(args)...)
}
// Error is equivalent to the global Error function, with the addition of prefix and data content from this Logger.
func (l *Logger) Error(args ...interface{}) {
l.print(errorLog, l.extendWithPfx(args)...)
}
// GetErrorEvent is equivalent to the global GetErrorEvent function, with the addition of prefix and data content from this Logger.
func (l *Logger) GetErrorEvent(args ...interface{}) Event {
return l.getEvent(errorLog, l.extendWithPfx(args)...)
}
// ErrorIf is equivalent to the global ErrorIf function, with the addition of prefix and data content from this Logger.
func (l *Logger) ErrorIf(err error, args ...interface{}) {
if err != nil {
if args != nil {
args = append(args, ": ")
}
args = append(args, err)
l.print(errorLog, l.extendWithPfx(args)...)
}
}
// Errorln is equivalent to the global Errorln function, with the addition of prefix and data content from this Logger.
func (l *Logger) Errorln(args ...interface{}) {
l.println(errorLog, l.extendWithPfx(args)...)
}
// Errorf is equivalent to the global Errorf function, with the addition of prefix and data content from this Logger.
func (l *Logger) Errorf(format string, args ...interface{}) {
l.printf(errorLog, l.pfx(format), l.extend(args)...)
}
// ErrorfIf is equivalent to the global ErrorfIf function, with the addition of prefix and data content from this Logger.
func (l *Logger) ErrorfIf(err error, format string, args ...interface{}) {
if err != nil {
format += ": %v"
args = append(args, err)
l.printf(errorLog, l.pfx(format), l.extend(args)...)
}
}
// Fatal is equivalent to the global Fatal function, with the addition of prefix and data content from this Logger.
func (l *Logger) Fatal(args ...interface{}) {
l.print(fatalLog, l.extendWithPfx(args)...)
}
// FatalIf is equivalent to the global FatalIf function, with the addition of prefix and data content from this Logger.
func (l *Logger) FatalIf(err error, args ...interface{}) {
if err != nil {
if args != nil {
errStr := ": " + err.Error()
args = append(args, errStr)
l.print(fatalLog, l.extendWithPfx(args)...)
} else {
l.print(fatalLog, l.extendWithPfx([]interface{}{
err,
})...)
}
}
}
// Fatalln is equivalent to the global Fatalln function, with the addition of prefix and data content from this Logger.
func (l *Logger) Fatalln(args ...interface{}) {
l.println(fatalLog, l.extendWithPfx(args)...)
}
// Fatalf is equivalent to the global Fatalf function, with the addition of prefix and data content from this Logger.
func (l *Logger) Fatalf(format string, args ...interface{}) {
l.printf(fatalLog, l.pfx(format), l.extend(args)...)
}
func (l *Logger) extendWithPfx(args []interface{}) []interface{} {
if l.prefix != "" {
args = append([]interface{}{
l.prefix,
}, args...)
}
return l.extend(args)
}
func (l *Logger) extend(args []interface{}) []interface{} {
for _, d := range l.data {
args = append(args, Data(d))
}
return args
}
func (l *Logger) pfx(log string) string {
return fmt.Sprintf("%v %v", l.prefix, log)
}