-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.go
More file actions
189 lines (162 loc) · 4.26 KB
/
Copy pathlogger.go
File metadata and controls
189 lines (162 loc) · 4.26 KB
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
// package logger is a package that provides a structured logger that's
// context.Context aware.
package logger
import (
"fmt"
"log"
"os"
"strings"
"context"
)
type Level int
const (
OFF Level = iota
ERROR
WARN
INFO
DEBUG
)
func ParseLevel(lvl string) Level {
switch strings.ToLower(lvl) {
case "off":
return OFF
case "error":
return ERROR
case "warn":
return WARN
case "info":
return INFO
case "debug":
return DEBUG
default:
return DEBUG
}
}
func FormatLevel(level Level) string {
switch level {
case OFF:
return "off"
case ERROR:
return "error"
case WARN:
return "warn"
case INFO:
return "info"
case DEBUG:
return "debug"
default:
return "debug"
}
}
// Logger represents a structured leveled logger.
type Logger interface {
Debug(msg string, pairs ...interface{})
Info(msg string, pairs ...interface{})
Warn(msg string, pairs ...interface{})
Error(msg string, pairs ...interface{})
With(pairs ...interface{}) Logger
}
var DefaultLogLevel = INFO
var DefaultLogger = New(log.New(os.Stdout, "[default] ", log.LstdFlags), DefaultLogLevel)
// logger is an implementation of the Logger interface backed by the stdlib's
// logging facility. This is a fairly naive implementation, and it's probably
// better to use something like https://github.com/inconshreveable/log15 which
// offers real structure logging.
type logger struct {
Level
*log.Logger
ctxPairs []interface{} // Contextual key value pairs that will be prepended to the log message.
}
// New wraps the log.Logger to implement the Logger interface.
func New(l *log.Logger, ll Level) Logger {
return &logger{
Logger: l,
Level: ll,
ctxPairs: []interface{}{},
}
}
// With returns a new logger with the given key value pairs added to each log message.
func (l *logger) With(pairs ...interface{}) Logger {
return &logger{
Logger: l.Logger,
Level: l.Level,
ctxPairs: append(l.ctxPairs, pairs...),
}
}
// Log logs the pairs in logfmt. It will treat consecutive arguments as a key
// value pair. Given the input:
func (l *logger) Log(level Level, msg string, pairs ...interface{}) {
if level <= l.Level {
msg = "status=" + FormatLevel(level) + " " + msg
m := l.message(pairs...)
l.Println(msg, m)
}
}
func (l *logger) Debug(msg string, pairs ...interface{}) { l.Log(DEBUG, msg, pairs...) }
func (l *logger) Info(msg string, pairs ...interface{}) { l.Log(INFO, msg, pairs...) }
func (l *logger) Error(msg string, pairs ...interface{}) { l.Log(ERROR, msg, pairs...) }
func (l *logger) Warn(msg string, pairs ...interface{}) { l.Log(WARN, msg, pairs...) }
func (l *logger) message(pairs ...interface{}) string {
pairs = append(l.ctxPairs, pairs...)
if len(pairs) == 1 {
return fmt.Sprintf("%v", pairs[0])
}
var parts []string
for i := 0; i < len(pairs); i += 2 {
// This conditional means that the pairs are uneven and we've
// reached the end of iteration. We treat the last value as a
// simple string message. Given an input pair as:
//
// ["key", "value", "message"]
//
// The output will be:
//
// key=value message
if len(pairs) == i+1 {
parts = append(parts, fmt.Sprintf("%v", pairs[i]))
} else {
parts = append(parts, fmt.Sprintf("%s=%v", pairs[i], pairs[i+1]))
}
}
return strings.Join(parts, " ")
}
// WithLogger inserts a log.Logger into the provided context.
func WithLogger(ctx context.Context, l Logger) context.Context {
return context.WithValue(ctx, loggerKey, l)
}
// FromContext returns a log.Logger from the context.
func FromContext(ctx context.Context) (Logger, bool) {
l, ok := ctx.Value(loggerKey).(Logger)
return l, ok
}
func Info(ctx context.Context, msg string, pairs ...interface{}) {
withLogger(ctx, func(l Logger) {
l.Info(msg, pairs...)
})
}
func Debug(ctx context.Context, msg string, pairs ...interface{}) {
withLogger(ctx, func(l Logger) {
l.Debug(msg, pairs...)
})
}
func Warn(ctx context.Context, msg string, pairs ...interface{}) {
withLogger(ctx, func(l Logger) {
l.Warn(msg, pairs...)
})
}
func Error(ctx context.Context, msg string, pairs ...interface{}) {
withLogger(ctx, func(l Logger) {
l.Error(msg, pairs...)
})
}
func withLogger(ctx context.Context, fn func(l Logger)) {
if l, ok := FromContext(ctx); ok {
fn(l)
} else {
fn(DefaultLogger)
}
}
type key int
const (
loggerKey key = iota
)