/
logging.go
359 lines (299 loc) · 8.48 KB
/
logging.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// A simple logging module that mimics the behavior of Python's logging module.
//
// All it does basically is wrap Go's logger with nice multi-level logging calls, and
// allows you to set the logging level of your app in runtime.
//
// Logging is done just like calling fmt.Sprintf:
//
// logging.Info("This object is %s and that is %s", obj, that)
//
// example output:
//
// [DBG 1670353253256778 instance.go:123] Setting config: projects
// [DBG 1670353253259897 subshell.go:95] Detected SHELL: zsh
// [DBG 1670353253259915 subshell.go:132] Using binary: /bin/zsh
package logging
// This package may NOT depend on failures (directly or indirectly)
import (
"bytes"
"fmt"
"io"
"log"
"os"
"path"
"runtime"
"runtime/debug"
"strings"
"time"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/osutils/stacktrace"
)
const (
DEBUG = 1
INFO = 2
WARNING = 4
WARN = 4
ERROR = 8
NOTICE = 16 //notice is like info but for really important stuff ;)
CRITICAL = 32
QUIET = ERROR | NOTICE | CRITICAL //setting for errors only
NORMAL = INFO | WARN | ERROR | NOTICE | CRITICAL // default setting - all besides debug
ALL = 255
NOTHING = 0
)
var levels_ascending = []int{DEBUG, INFO, WARNING, ERROR, NOTICE, CRITICAL}
var LevelsByName = map[string]int{
"DEBUG": DEBUG,
"INFO": INFO,
"WARNING": WARN,
"WARN": WARN,
"ERROR": ERROR,
"NOTICE": NOTICE,
"CRITICAL": CRITICAL,
"QUIET": QUIET,
"NORMAL": NORMAL,
"ALL": ALL,
"NOTHING": NOTHING,
}
// default logging level is ALL
var level int = ALL
// Set the logging level.
//
// Contrary to Python that specifies a minimal level, this logger is set with a bit mask
// of active levels.
//
// e.g. for INFO and ERROR use:
//
// SetLevel(logging.INFO | logging.ERROR)
//
// For everything but debug and info use:
//
// SetLevel(logging.ALL &^ (logging.INFO | logging.DEBUG))
func SetLevel(l int) {
level = l
}
// Set a minimal level for loggin, setting all levels higher than this level as well.
//
// the severity order is DEBUG, INFO, WARNING, ERROR, CRITICAL
func SetMinimalLevel(l int) {
newLevel := 0
for _, level := range levels_ascending {
if level >= l {
newLevel |= level
}
}
SetLevel(newLevel)
}
// Set minimal level by string, useful for config files and command line arguments. Case insensitive.
//
// Possible level names are DEBUG, INFO, WARNING, ERROR, NOTICE, CRITICAL
func SetMinimalLevelByName(l string) error {
l = strings.ToUpper(strings.Trim(l, " "))
level, found := LevelsByName[l]
if !found {
Error("Could not set level - not found level %s", l)
return fmt.Errorf("Invalid level %s", l)
}
SetMinimalLevel(level)
return nil
}
// Set the output writer. for now it just wraps log.SetOutput()
func SetOutput(w io.Writer) {
log.SetOutput(w)
}
// a pluggable logger interface
type LoggingHandler interface {
SetFormatter(Formatter)
SetVerbose(bool)
Output() io.Writer
Emit(ctx *MessageContext, message string, args ...interface{}) error
Printf(msg string, args ...interface{})
Close()
}
type standardHandler struct {
formatter Formatter
}
func (l *standardHandler) SetFormatter(f Formatter) {
l.formatter = f
}
func (l *standardHandler) SetVerbose(v bool) {
}
func (l *standardHandler) Output() io.Writer {
return nil
}
// default handling interface - just
func (l *standardHandler) Emit(ctx *MessageContext, message string, args ...interface{}) error {
fmt.Fprintln(os.Stderr, l.formatter.Format(ctx, message, args...))
return nil
}
// Printf satifies a Logger interface allowing us to funnel our
// logging handlers to 3rd party libraries
func (l *standardHandler) Printf(msg string, args ...interface{}) {
logMsg := fmt.Sprintf("Third party log message: %s", msg)
l.Emit(getContext("DBG", 1), logMsg, args...)
}
func (l *standardHandler) Close() {}
var currentHandler LoggingHandler = &standardHandler{
DefaultFormatter,
}
// Set the current handler of the library. We currently support one handler, but it might be nice to have more
func SetHandler(h LoggingHandler) {
currentHandler = h
}
func CurrentHandler() LoggingHandler {
return currentHandler
}
type MessageContext struct {
Level string
File string
Line int
TimeStamp time.Time
}
// get the stack (line + file) context to return the caller to the log
func getContext(level string, skipDepth int) *MessageContext {
_, file, line, _ := runtime.Caller(skipDepth)
file = path.Base(file)
return &MessageContext{
Level: level,
File: file,
TimeStamp: time.Now(),
Line: line,
}
}
// Output debug logging messages
func Debug(msg string, args ...interface{}) {
if level&DEBUG != 0 {
writeMessage("DBG", msg, args...)
}
}
type writer struct{}
func (w *writer) Write(p []byte) (n int, err error) {
if level&DEBUG != 0 {
writeMessage("DBG", string(p))
}
return len(p), nil
}
// format the message
func writeMessage(level string, msg string, args ...interface{}) {
writeMessageDepth(4, level, msg, args...)
}
// TailSize specifies the number of logged bytes to keep for use with Tail.
const TailSize = 5000
var logTail *ringBuffer
var tailLogger *log.Logger
func writeToLogTail(ctx *MessageContext, msg string, args ...interface{}) {
if tailLogger == nil {
logTail = newRingBuffer(TailSize)
tailLogger = log.New(logTail, "", log.LstdFlags)
}
tailLogger.Println(DefaultFormatter.Format(ctx, msg, args...))
}
// ReadTail returns as a string the last TailSize bytes written by this logger.
func ReadTail() string {
if logTail == nil {
return ""
}
return logTail.Read()
}
func writeMessageDepth(depth int, level string, msg string, args ...interface{}) {
ctx := getContext(level, depth)
// We go over the args, and replace any function pointer with the signature
// func() interface{} with the return value of executing it now.
// This allows lazy evaluation of arguments which are return values
for i, arg := range args {
switch arg := arg.(type) {
case func() interface{}:
args[i] = arg
default:
}
}
err := currentHandler.Emit(ctx, msg, args...)
if err != nil {
printLogError(err, ctx, msg, args...)
}
writeToLogTail(ctx, msg, args...)
}
func printLogError(err error, ctx *MessageContext, msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, "Error writing log message: %s\n", errs.JoinMessage(err))
fmt.Fprintln(os.Stderr, DefaultFormatter.Format(ctx, msg, args...))
}
// output INFO level messages
func Info(msg string, args ...interface{}) {
if level&INFO != 0 {
writeMessage("INF", msg, args...)
}
}
// Output WARNING level messages
func Warning(msg string, args ...interface{}) {
if level&WARN != 0 {
writeMessage("WRN", msg, args...)
}
}
// Output ERROR level messages
// This should be used sparingly, as multilog.Error() is preferred.
func Error(msg string, args ...interface{}) {
if level&ERROR != 0 {
writeMessage("ERR", msg+"\n\nStacktrace: "+stacktrace.Get().String()+"\n", args...)
}
}
// Same as Error() but without a stacktrace.
func ErrorNoStacktrace(msg string, args ...interface{}) {
if level&ERROR != 0 {
writeMessage("ERR", msg, args...)
}
}
// Output NOTICE level messages
func Notice(msg string, args ...interface{}) {
if level&NOTICE != 0 {
writeMessage("NOT", msg, args...)
}
}
// Output a CRITICAL level message while showing a stack trace
// This should be called sparingly, as multilog.Critical() is preferred.
func Critical(msg string, args ...interface{}) {
if level&CRITICAL != 0 {
writeMessage("CRT", msg, args...)
log.Println(string(debug.Stack()))
}
}
// Raise a PANIC while writing the stack trace to the log
func Panic(msg string, args ...interface{}) {
log.Println(string(debug.Stack()))
log.Panicf(msg, args...)
}
func Close() {
currentHandler.Close()
}
func init() {
log.SetFlags(0)
}
// bridge bridges the logger and the default go log, with a given level
type bridge struct {
level int
levelName string
}
func (lb bridge) Write(p []byte) (n int, err error) {
if level&lb.level != 0 {
writeMessageDepth(6, lb.levelName, string(bytes.TrimRight(p, "\r\n")))
}
return len(p), nil
}
// BridgeStdLog bridges all messages written using the standard library's log.Print* and makes them output
// through this logger, at a given level.
func BridgeStdLog(level int) {
for k, l := range LevelsByName {
if l == level {
b := bridge{
level: l,
levelName: k,
}
log.SetOutput(b)
}
}
}
func handlePanics(err interface{}) {
if err == nil {
return
}
fmt.Fprintf(os.Stderr, "Failed to log error. Please report this on the forums if it keeps happening. Error: %v\n", err)
}