-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
135 lines (109 loc) · 3.34 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
package log
import stdlog "log"
// 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
}
// 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.
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)
}
func (l *Logger) WithMemStats() *Entry {
return NewEntry(l).WithField("memstats", Fn(getMemStats))
}
// Debug level message.
func (l *Logger) Debug(v ...interface{}) {
NewEntry(l).Debug(sprintlnn(v...))
}
// Info level message.
func (l *Logger) Info(v ...interface{}) {
NewEntry(l).Info(v...)
}
// Warn level message.
func (l *Logger) Warn(v ...interface{}) {
NewEntry(l).Warn(v...)
}
// Error level message.
func (l *Logger) Error(v ...interface{}) {
NewEntry(l).Error(v...)
}
// Fatal level message, followed by an exit.
func (l *Logger) Fatal(v ...interface{}) {
NewEntry(l).Fatal(v...)
}
// 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 l.Handler == nil {
return
}
if err := l.Handler.HandleLog(e.finalize(level, msg)); err != nil {
stdlog.Printf("error logging: %s", err)
}
}