-
Notifications
You must be signed in to change notification settings - Fork 1
/
clog.go
298 lines (253 loc) · 9.66 KB
/
clog.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*******************************************************************************
* Copyright (c) 2020, 2021 Genome Research Ltd.
*
* Author: Sendu Bala <sb10@sanger.ac.uk>, <ac55@sanger.ac.uk>
* Based on: https://blog.gopheracademy.com/advent-2016/context-logging/
* CallerInfoHandler based on code from github.com/sb10/l15h
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/
// package clog is used to do contextual logging with a global logger.
package clog
import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"github.com/go-stack/stack"
log "github.com/inconshreveable/log15"
"github.com/sb10/l15h"
)
// osExit is used to disable real os.Exit for testing purposes when calling
// Fatal().
var osExit = os.Exit
// init sets our default logging syle.
func init() {
ToDefault()
}
// ToDefault sets the global logger to log to STDERR at the "warn" level.
func ToDefault() {
toOutputAtLevel(log.StderrHandler, log.LvlWarn)
}
// ToDefaultAtLevel sets the global logger to log to STDERR at the given level.
func ToDefaultAtLevel(lvl string) {
toStderrAtLevel(lvlFromString(lvl))
}
// toStderrAtLevel sets the handler of the global logger at the given level to
// log to STDERR.
func toStderrAtLevel(lvl log.Lvl) {
h := log.StreamHandler(os.Stderr, log.TerminalFormat())
setRootHandler(log.LvlFilterHandler(lvl, h))
}
// ToHandlerAtLevel sets the default logger to a given custom handler at the
// given level.
// Eg. to log to syslog
// ...
// handler, _ := log15.SyslogHandler(syslog.LOG_USER,
// "wrrunner", log15.LogfmtFormat())
// clog.ToHandlerAtLevel(handler, "info")
// ...
func ToHandlerAtLevel(outputHandler log.Handler, lvl string) {
toOutputAtLevel(outputHandler, lvlFromString(lvl))
}
// GetHandler returns the global logger handler used for all logging.
func GetHandler() log.Handler {
return log.Root().GetHandler()
}
// toOutputAtLevel sets the handler of the global logger to filter on the given
// level, add caller info, and output to the given handler.
func toOutputAtLevel(outputHandler log.Handler, lvl log.Lvl) {
h := createFilteredInfoHandler(outputHandler, lvl)
setRootHandler(h)
}
// CallerInfoHandler returns a Handler that, at the Debug, Warn and Error
// levels, adds the file and line number of the calling function to the context
// with key "caller". At the Crit levels it instead adds a stack trace to the
// context with key "stack". The stack trace is formatted as a space separated
// list of call sites inside matching []'s. The most recent call site is listed
// first.
func CallerInfoHandler(h log.Handler) log.Handler {
return log.FuncHandler(func(r *log.Record) error {
s := stack.Trace().TrimBelow(r.Call).TrimRuntime()
if len(s) > 0 {
switch r.Lvl {
case log.LvlInfo:
break
case log.LvlDebug, log.LvlWarn, log.LvlError:
var call stack.Call
if len(s) > 1 {
call = s[1]
} else {
call = s[0]
}
path := filepath.Join(fmt.Sprintf("%k", call), fmt.Sprintf("%v", call))
r.Ctx = append(r.Ctx, "caller", path)
case log.LvlCrit:
r.Ctx = append(r.Ctx, "stack", fmt.Sprintf("%+v", s))
}
}
return h.Log(r)
})
}
// createFilteredInfoHandler wraps the given output handler in handlers that add
// caller info and filters on the given level.
func createFilteredInfoHandler(outputHandler log.Handler, lvl log.Lvl) log.Handler {
return log.LvlFilterHandler(
lvl,
CallerInfoHandler(
outputHandler,
),
)
}
// setRootHandler sets the given handler as the root handler that all logging
// will use.
func setRootHandler(h log.Handler) {
log.Root().SetHandler(h)
}
// ToBufferAtLevel sets the global logger to log to the returned
// bytes.Buffer at the given level.
func ToBufferAtLevel(lvl string) *bytes.Buffer {
buff := new(bytes.Buffer)
toOutputAtLevel(log.StreamHandler(buff, log.LogfmtFormat()), lvlFromString(lvl))
return buff
}
// ContextWithFileHandler returns a context that will log to the given file at
// the given level.
func ContextWithFileHandler(ctx context.Context, path, lvl string) (context.Context, error) {
fh, err := CreateFileHandlerAtLevel(path, lvl)
if err != nil {
return nil, err
}
return ContextWithLogHandler(ctx, fh), nil
}
// CreateFileHandlerAtLevel returns a log15 file handler at the given level.
func CreateFileHandlerAtLevel(path, lvl string) (log.Handler, error) {
fh, err := log.FileHandler(path, log.LogfmtFormat())
if err != nil {
return nil, err
}
return createFilteredInfoHandler(fh, lvlFromString(lvl)), nil
}
// AddHandler adds the given log15 handler to global logger.
func AddHandler(handler log.Handler) {
l15h.AddHandler(log.Root(), handler)
}
// ToFileAtLevel sets the global logger to log to a file at the given path
// and at the given level.
func ToFileAtLevel(path, lvl string) error {
fh, err := CreateFileHandlerAtLevel(path, lvl)
if err != nil {
return err
}
setRootHandler(fh)
return nil
}
// lvlFromString returns a log.Lvl for the given string. Valid lvls are
// "debug"|"dbug", "info", "warn", "error"|"eror", "crit". Invalid lvls return
// LvlDebug.
func lvlFromString(lvl string) log.Lvl {
logLevel, err := log.LvlFromString(lvl)
if err != nil {
return log.LvlDebug
}
return logLevel
}
// logger returns the global logger with as much context as possible.
func logger(ctx context.Context) log.Logger {
logger := log.Root()
if ctx != nil {
logger = addStringKeyToLogger(ctx, logger, retrySetKey, "retryset")
logger = addStringKeyToLogger(ctx, logger, retryActivityKey, "retryactivity")
logger = addIntKeyToLogger(ctx, logger, retryNumKey, "retrynum")
logger = addStringKeyToLogger(ctx, logger, contextJobKey, "jobkey")
logger = addStringKeyToLogger(ctx, logger, contextServerID, "serverid")
logger = addStringKeyToLogger(ctx, logger, contextSchedulerType, "schedulertype")
logger = addStringKeyToLogger(ctx, logger, contextCloudType, "cloudtype")
logger = addStringKeyToLogger(ctx, logger, contextCallValue, "callvalue")
logger = addStringKeyToLogger(ctx, logger, contextServerFlavor, "serverflavor")
logger = addHandlerToLogger(ctx, logger)
}
return logger
}
// addStringKeyToLogger checks if the given string key is set in the logger and
// returns a new logger with that context under the logger key if so.
func addStringKeyToLogger(ctx context.Context, logger log.Logger, key correlationIDType, loggerKey string) log.Logger {
if val, ok := ctx.Value(key).(string); ok {
logger = logger.New(loggerKey, val)
}
return logger
}
// addIntKeyToLogger checks if the given int key is set in the logger and
// returns a new logger with that context under the logger key if so.
func addIntKeyToLogger(ctx context.Context, logger log.Logger, key correlationIDType, loggerKey string) log.Logger {
if val, ok := ctx.Value(key).(int); ok {
logger = logger.New(loggerKey, val)
}
return logger
}
// addHandlerToLogger checks if a handler has been set in the context and
// sets the logger's handler to it.
func addHandlerToLogger(ctx context.Context, logger log.Logger) log.Logger {
if val, ok := ctx.Value(contextLogHandler).(log.Handler); ok {
logger = logger.New()
logger.SetHandler(val)
}
return logger
}
// Debug logs the given message with context and args to the global logger at
// the debug level. Caller info is included.
func Debug(ctx context.Context, msg string, args ...interface{}) {
logger(ctx).Debug(msg, args...)
}
// Info logs the given message with context and args to the global logger at
// the info level.
func Info(ctx context.Context, msg string, args ...interface{}) {
logger(ctx).Info(msg, args...)
}
// Warn logs the given message with context and args to the global logger at
// the warn level. Caller info is included.
func Warn(ctx context.Context, msg string, args ...interface{}) {
logger(ctx).Warn(msg, args...)
}
// Error logs the given message with context and args to the global logger at
// the error level. Caller info is included.
func Error(ctx context.Context, msg string, args ...interface{}) {
logger(ctx).Error(msg, args...)
}
// Crit logs the given message with context and args to the global logger at
// the error level. A stack trace is included.
func Crit(ctx context.Context, msg string, args ...interface{}) {
logger(ctx).Crit(msg, args...)
}
// Fatal logs the given message with context and args to the global logger at
// the error level before exiting. 'fatal' is set true in stack trace.
//
// If the WR_FATAL_EXIT_TEST environment variable is set to 1, we don't
// actually exit.
func Fatal(ctx context.Context, msg string, args ...interface{}) {
args = append(args, "fatal", true)
logger(ctx).Crit(msg, args...)
if os.Getenv("WR_FATAL_EXIT_TEST") == "1" {
defer func() { osExit = os.Exit }()
osExit = func(int) {}
}
osExit(1)
}