-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
logger.go
156 lines (127 loc) · 3.7 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
package log
import (
stdlog "log"
"sort"
"time"
)
// 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())
}
// 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)
}
// WithDuration returns a new entry with the "duration" field set
// to the given duration in milliseconds.
func (l *Logger) WithDuration(d time.Duration) *Entry {
return NewEntry(l).WithDuration(d)
}
// WithError returns a new entry with the "error" set to `err`.
func (l *Logger) WithError(err error) *Entry {
return NewEntry(l).WithError(err)
}
// 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)
}
// 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)
}
// Fatal level message, followed by an exit.
func (l *Logger) Fatal(msg string) {
NewEntry(l).Fatal(msg)
}
// 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...)
}
// 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...)
}
// Fatalf level formatted message, followed by an exit.
func (l *Logger) Fatalf(msg string, v ...interface{}) {
NewEntry(l).Fatalf(msg, v...)
}
// 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)
}
}