-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
206 lines (165 loc) · 4.83 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
202
203
204
205
206
package log
import (
stdlog "log"
"sort"
)
// assert interface compliance.
var _ Interface = (*Logger)(nil)
// Fielder is an interface for providing fields to custom types.
type Fielder interface {
Fields() Fields
}
// Fields represents a map of entry level data used for structured logging.
type Fields map[string]interface{}
// Fields implements Fielder.
func (f Fields) Fields() Fields {
return f
}
// Get field value by name.
func (f Fields) Get(name string) interface{} {
return f[name]
}
// Names returns field names sorted.
func (f Fields) Names() (v []string) {
for k := range f {
v = append(v, k)
}
sort.Strings(v)
return
}
// The HandlerFunc type is an adapter to allow the use of ordinary functions as
// log handlers. If f is a function with the appropriate signature,
// HandlerFunc(f) is a Handler object that calls f.
type HandlerFunc func(*Entry) error
// HandleLog calls f(e).
func (f HandlerFunc) HandleLog(e *Entry) error {
return f(e)
}
// Handler is used to handle log events, outputting them to
// stdio or sending them to remote services. See the "handlers"
// directory for implementations.
//
// It is left up to Handlers to implement thread-safety.
type Handler interface {
HandleLog(*Entry) error
}
// Logger represents a logger with configurable Level and Handler.
type Logger struct {
Handler Handler
Level Level
}
// WithFields returns a new entry with `fields` set.
func (l *Logger) WithFields(fields Fielder) *Entry {
return NewEntry(l).WithFields(fields.Fields())
}
// SetEnvProject returns a entry with env, project and hostname fields set.
func (l *Logger) SetEnvProject(env string, project string) *Entry {
return NewEntry(l).SetEnvProject(env, project)
}
// WithField returns a new entry with the `key` and `value` set.
//
// Note that the `key` should not have spaces in it - use camel
// case or underscores
func (l *Logger) WithField(key string, value interface{}) *Entry {
return NewEntry(l).WithField(key, value)
}
// WithError returns a new entry with the "error" set to `err`.
func (l *Logger) WithError(err error) *Entry {
return NewEntry(l).WithError(err)
}
/********* Log simple *********/
/* WARNING: This part is autogenerated! Do not change! */
// Debug level message.
func (l *Logger) Debug(msg string) {
NewEntry(l).Debug(msg)
}
// Info level message.
func (l *Logger) Info(msg string) {
NewEntry(l).Info(msg)
}
// Notice level message.
func (l *Logger) Notice(msg string) {
NewEntry(l).Notice(msg)
}
// Warn level message.
func (l *Logger) Warn(msg string) {
NewEntry(l).Warn(msg)
}
// Error level message.
func (l *Logger) Error(msg string) {
NewEntry(l).Error(msg)
}
// Critical level message.
func (l *Logger) Critical(msg string) {
NewEntry(l).Critical(msg)
}
// Alert level message.
func (l *Logger) Alert(msg string) {
NewEntry(l).Alert(msg)
}
// Fatal level message.
func (l *Logger) Fatal(msg string) {
NewEntry(l).Fatal(msg)
}
// Emergency level message.
func (l *Logger) Emergency(msg string) {
NewEntry(l).Emergency(msg)
}
/* END OF WARNING */
/********* End Log simple *********/
/********* Log formated *********/
/* WARNING: This part is autogenerated! Do not change! */
// Debugf level formatted message.
func (l *Logger) Debugf(msg string, v ...interface{}) {
NewEntry(l).Debugf(msg, v...)
}
// Infof level formatted message.
func (l *Logger) Infof(msg string, v ...interface{}) {
NewEntry(l).Infof(msg, v...)
}
// Noticef level formatted message.
func (l *Logger) Noticef(msg string, v ...interface{}) {
NewEntry(l).Noticef(msg, v...)
}
// Warnf level formatted message.
func (l *Logger) Warnf(msg string, v ...interface{}) {
NewEntry(l).Warnf(msg, v...)
}
// Errorf level formatted message.
func (l *Logger) Errorf(msg string, v ...interface{}) {
NewEntry(l).Errorf(msg, v...)
}
// Criticalf level formatted message.
func (l *Logger) Criticalf(msg string, v ...interface{}) {
NewEntry(l).Criticalf(msg, v...)
}
// Alertf level formatted message.
func (l *Logger) Alertf(msg string, v ...interface{}) {
NewEntry(l).Alertf(msg, v...)
}
// Fatalf level formatted message.
func (l *Logger) Fatalf(msg string, v ...interface{}) {
NewEntry(l).Fatalf(msg, v...)
}
// Emergencyf level formatted message.
func (l *Logger) Emergencyf(msg string, v ...interface{}) {
NewEntry(l).Emergencyf(msg, v...)
}
/* END OF WARNING */
/********* End Log formated *********/
// Trace returns a new entry with a Stop method to fire off
// a corresponding completion log, useful with defer.
func (l *Logger) Trace(msg string) *Entry {
return NewEntry(l).Trace(msg)
}
// log the message, invoking the handler. We clone the entry here
// to bypass the overhead in Entry methods when the level is not
// met.
func (l *Logger) log(level Level, e *Entry, msg string) {
if level < l.Level {
return
}
if err := l.Handler.HandleLog(e.finalize(level, msg)); err != nil {
stdlog.Printf("error logging: %s", err)
}
}