forked from erinpentecost/gou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
591 lines (525 loc) · 15.1 KB
/
log.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
package gou
import (
"context"
"fmt"
"log"
"os"
"runtime"
"strings"
"sync"
"time"
)
const (
NOLOGGING = -1
FATAL = 0
ERROR = 1
WARN = 2
INFO = 3
DEBUG = 4
)
/*
https://github.com/mewkiz/pkg/tree/master/term
RED = '\033[0;1;31m'
GREEN = '\033[0;1;32m'
YELLOW = '\033[0;1;33m'
BLUE = '\033[0;1;34m'
MAGENTA = '\033[0;1;35m'
CYAN = '\033[0;1;36m'
WHITE = '\033[0;1;37m'
DARK_MAGENTA = '\033[0;35m'
ANSI_RESET = '\033[0m'
LogColor = map[int]string{FATAL: "\033[0m\033[37m",
ERROR: "\033[0m\033[31m",
WARN: "\033[0m\033[33m",
INFO: "\033[0m\033[32m",
DEBUG: "\033[0m\033[34m"}
\e]PFdedede
*/
var (
LogLevel int = ERROR
EMPTY struct{}
ErrLogLevel int = ERROR
logger *log.Logger
customLogger LoggerCustom
loggerErr *log.Logger
LogColor = map[int]string{FATAL: "\033[0m\033[37m",
ERROR: "\033[0m\033[31m",
WARN: "\033[0m\033[33m",
INFO: "\033[0m\033[35m",
DEBUG: "\033[0m\033[34m"}
LogPrefix = map[int]string{
FATAL: "[FATAL] ",
ERROR: "[ERROR] ",
WARN: "[WARN] ",
INFO: "[INFO] ",
DEBUG: "[DEBUG] ",
}
logContextKey = "log_prefix"
escapeNewlines bool = false
postFix = "" //\033[0m
LogLevelWords map[string]int = map[string]int{"fatal": 0, "error": 1, "warn": 2, "info": 3, "debug": 4, "none": -1}
logThrottles = make(map[string]*Throttler)
throttleMu sync.Mutex
)
// LoggerCustom defines custom interface for logger implementation
type LoggerCustom interface {
Log(depth, logLevel int, msg string, fields map[string]interface{})
}
// Setup default logging to Stderr, equivalent to:
//
// gou.SetLogger(log.New(os.Stderr, "", log.Ltime|log.Lshortfile), "debug")
func SetupLogging(lvl string) {
SetLogger(log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile|log.Lmicroseconds), strings.ToLower(lvl))
}
// Setup default logging to Stderr, equivalent to:
//
// gou.SetLogger(log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile|log.Lmicroseconds), level)
func SetupLoggingLong(lvl string) {
SetLogger(log.New(os.Stderr, "", log.LstdFlags|log.Llongfile|log.Lmicroseconds), strings.ToLower(lvl))
}
// SetupLoggingFile writes logs to the file object parameter.
func SetupLoggingFile(f *os.File, lvl string) {
SetLogger(log.New(f, "", log.LstdFlags|log.Lshortfile|log.Lmicroseconds), strings.ToLower(lvl))
}
// SetCustomLogger sets the logger to a custom logger
func SetCustomLogger(cl LoggerCustom) {
customLogger = cl
}
// GetCustomLogger returns the custom logger if initialized
func GetCustomLogger() LoggerCustom {
return customLogger
}
// Setup colorized output if this is a terminal
func SetColorIfTerminal() {
if IsTerminal() {
SetColorOutput()
}
}
// Setup colorized output
func SetColorOutput() {
for lvl, color := range LogColor {
LogPrefix[lvl] = color
}
postFix = "\033[0m"
}
//Set whether to escape newline characters in log messages
func SetEscapeNewlines(en bool) {
escapeNewlines = en
}
// Setup default log output to go to a dev/null
//
// log.SetOutput(new(DevNull))
func DiscardStandardLogger() {
log.SetOutput(new(DevNull))
}
// you can set a logger, and log level,most common usage is:
//
// gou.SetLogger(log.New(os.Stdout, "", log.LstdFlags), "debug")
//
// loglevls: debug, info, warn, error, fatal
// Note, that you can also set a separate Error Log Level
func SetLogger(l *log.Logger, logLevel string) {
logger = l
LogLevelSet(logLevel)
}
func GetLogger() *log.Logger {
return logger
}
// you can set a logger, and log level. this is for errors, and assumes
// you are logging to Stderr (seperate from stdout above), allowing you to seperate
// debug&info logging from errors
//
// gou.SetLogger(log.New(os.Stderr, "", log.LstdFlags), "debug")
//
// loglevls: debug, info, warn, error, fatal
func SetErrLogger(l *log.Logger, logLevel string) {
loggerErr = l
if lvl, ok := LogLevelWords[logLevel]; ok {
ErrLogLevel = lvl
}
}
func GetErrLogger() *log.Logger {
return logger
}
// sets the log level from a string
func LogLevelSet(levelWord string) {
if lvl, ok := LogLevelWords[levelWord]; ok {
LogLevel = lvl
}
}
// NewContext returns a new Context carrying contextual log message
// that gets prefixed to log statements.
func NewContext(ctx context.Context, msg string) context.Context {
return context.WithValue(ctx, logContextKey, msg)
}
// NewContextWrap returns a new Context carrying contextual log message
// that gets prefixed to log statements.
func NewContextWrap(ctx context.Context, msg string) context.Context {
logContext, ok := ctx.Value(logContextKey).(string)
if ok {
return context.WithValue(ctx, logContextKey, fmt.Sprintf("%s %s", logContext, msg))
}
return context.WithValue(ctx, logContextKey, msg)
}
// FromContext extracts the Log Context prefix from context
func FromContext(ctx context.Context) string {
logContext, _ := ctx.Value(logContextKey).(string)
return logContext
}
// Log at debug level
func Debug(v ...interface{}) {
if LogLevel >= 4 {
DoLog(3, DEBUG, fmt.Sprint(v...))
}
}
// Debug log formatted
func Debugf(format string, v ...interface{}) {
if LogLevel >= 4 {
DoLog(3, DEBUG, fmt.Sprintf(format, v...))
}
}
// Debug log formatted context writer
func DebugCtx(ctx context.Context, format string, v ...interface{}) {
if LogLevel >= 4 {
lc := FromContext(ctx)
if len(lc) > 0 {
format = fmt.Sprintf("%s %s", lc, format)
}
DoLog(3, DEBUG, fmt.Sprintf(format, v...))
}
}
func DebugT(lineCt int) {
if LogLevel >= 4 {
DoLog(3, DEBUG, fmt.Sprint("\n", PrettyStack(lineCt)))
}
}
// Log at info level
func Info(v ...interface{}) {
if LogLevel >= 3 {
DoLog(3, INFO, fmt.Sprint(v...))
}
}
// info log formatted
func Infof(format string, v ...interface{}) {
if LogLevel >= 3 {
DoLog(3, INFO, fmt.Sprintf(format, v...))
}
}
// Info log formatted context writer
func InfoCtx(ctx context.Context, format string, v ...interface{}) {
if LogLevel >= 3 {
lc := FromContext(ctx)
if len(lc) > 0 {
format = fmt.Sprintf("%s %s", lc, format)
}
DoLog(3, INFO, fmt.Sprintf(format, v...))
}
}
// Info Trace
func InfoT(lineCt int) {
if LogLevel >= 3 {
DoLog(3, INFO, fmt.Sprint("\n", PrettyStack(lineCt)))
}
}
// Log at warn level
func Warn(v ...interface{}) {
if LogLevel >= 2 {
DoLog(3, WARN, fmt.Sprint(v...))
}
}
// Warn log formatted
func Warnf(format string, v ...interface{}) {
if LogLevel >= 2 {
DoLog(3, WARN, fmt.Sprintf(format, v...))
}
}
// Warn log formatted context writer
func WarnCtx(ctx context.Context, format string, v ...interface{}) {
if LogLevel >= 2 {
lc := FromContext(ctx)
if len(lc) > 0 {
format = fmt.Sprintf("%s %s", lc, format)
}
DoLog(3, WARN, fmt.Sprintf(format, v...))
}
}
// Warn Trace
func WarnT(lineCt int) {
if LogLevel >= 2 {
DoLog(3, WARN, fmt.Sprint("\n", PrettyStack(lineCt)))
}
}
// Log at error level
func Error(v ...interface{}) {
if LogLevel >= 1 {
DoLog(3, ERROR, fmt.Sprint(v...))
}
}
// Error log formatted
func Errorf(format string, v ...interface{}) {
if LogLevel >= 1 {
DoLog(3, ERROR, fmt.Sprintf(format, v...))
}
}
// Error log formatted context writer
func ErrorCtx(ctx context.Context, format string, v ...interface{}) {
if LogLevel >= 1 {
lc := FromContext(ctx)
if len(lc) > 0 {
format = fmt.Sprintf("%s %s", lc, format)
}
DoLog(3, ERROR, fmt.Sprintf(format, v...))
}
}
// Log this error, and return error object
func LogErrorf(format string, v ...interface{}) error {
err := fmt.Errorf(format, v...)
if LogLevel >= 1 {
DoLog(3, ERROR, err.Error())
}
return err
}
// Log to logger if setup
//
// Log(ERROR, "message")
func Log(logLvl int, v ...interface{}) {
if LogLevel >= logLvl {
DoLog(3, logLvl, fmt.Sprint(v...))
}
}
// Log to logger if setup, grab a stack trace and add that as well
//
// u.LogTracef(u.ERROR, "message %s", varx)
//
func LogTracef(logLvl int, format string, v ...interface{}) {
if LogLevel >= logLvl {
// grab a stack trace
stackBuf := make([]byte, 6000)
stackBufLen := runtime.Stack(stackBuf, false)
stackTraceStr := string(stackBuf[0:stackBufLen])
parts := strings.Split(stackTraceStr, "\n")
if len(parts) > 1 {
v = append(v, strings.Join(parts[3:], "\n"))
}
DoLog(3, logLvl, fmt.Sprintf(format+"\n%v", v...))
}
}
// Log to logger if setup, grab a stack trace and add that as well
//
// u.LogTracef(u.ERROR, "message %s", varx)
//
func LogTraceDf(logLvl, lineCt int, format string, v ...interface{}) {
if LogLevel >= logLvl {
// grab a stack trace
stackBuf := make([]byte, 6000)
stackBufLen := runtime.Stack(stackBuf, false)
stackTraceStr := string(stackBuf[0:stackBufLen])
parts := strings.Split(stackTraceStr, "\n")
if len(parts) > 1 {
if (len(parts) - 3) > lineCt {
parts = parts[3 : 3+lineCt]
parts2 := make([]string, 0, len(parts)/2)
for i := 1; i < len(parts); i = i + 2 {
parts2 = append(parts2, parts[i])
}
v = append(v, strings.Join(parts2, "\n"))
//v = append(v, strings.Join(parts[3:3+lineCt], "\n"))
} else {
v = append(v, strings.Join(parts[3:], "\n"))
}
}
DoLog(3, logLvl, fmt.Sprintf(format+"\n%v", v...))
}
}
// PrettyStack is a helper to pull stack-trace, prettify it.
func PrettyStack(lineCt int) string {
stackBuf := make([]byte, 10000)
stackBufLen := runtime.Stack(stackBuf, false)
stackTraceStr := string(stackBuf[0:stackBufLen])
parts := strings.Split(stackTraceStr, "\n")
if len(parts) > 3 {
parts = parts[2:]
parts2 := make([]string, 0, len(parts)/2)
for i := 3; i < len(parts)-1; i++ {
if !strings.HasSuffix(parts[i], ")") && !strings.HasPrefix(parts[i], "/usr/local") {
parts2 = append(parts2, parts[i])
}
}
if len(parts2) > lineCt {
return strings.Join(parts2[0:lineCt], "\n")
}
return strings.Join(parts2, "\n")
}
return stackTraceStr
}
// Throttle logging based on key, such that key would never occur more than
// @limit times per hour
//
// LogThrottleKey(u.ERROR, 1,"error_that_happens_a_lot" "message %s", varx)
//
func LogThrottleKey(logLvl, limit int, key, format string, v ...interface{}) {
if LogLevel < logLvl {
return
}
doLogThrottle(4, logLvl, limit, key, fmt.Sprintf(format, v...))
}
// LogThrottleKeyCtx log formatted context writer
func LogThrottleKeyCtx(ctx context.Context, logLvl, limit int, key, format string, v ...interface{}) {
if LogLevel < logLvl {
return
}
lc := FromContext(ctx)
if len(lc) > 0 {
format = fmt.Sprintf("%s %s", lc, format)
}
doLogThrottle(4, logLvl, limit, key, fmt.Sprintf(format, v...))
}
// LogThrottleD logging based on @format as a key, such that key would never occur more than
// @limit times per hour
//
// LogThrottle(u.ERROR, 1, "message %s", varx)
//
func LogThrottle(logLvl, limit int, format string, v ...interface{}) {
if LogLevel < logLvl {
return
}
doLogThrottle(4, logLvl, limit, format, fmt.Sprintf(format, v...))
}
// LogThrottleCtx log formatted context writer, limits to @limit/hour.
func LogThrottleCtx(ctx context.Context, logLvl, limit int, format string, v ...interface{}) {
if LogLevel < logLvl {
return
}
lc := FromContext(ctx)
if len(lc) > 0 {
format = fmt.Sprintf("%s %s", lc, format)
}
doLogThrottle(4, logLvl, limit, format, fmt.Sprintf(format, v...))
}
// LogThrottleD Throttle logging based on @format as a key, such that key would never
// occur more than @limit times per hour
//
// LogThrottleD(5, u.ERROR, 1, "message %s", varx)
//
func LogThrottleD(depth, logLvl, limit int, format string, v ...interface{}) {
if LogLevel < logLvl {
return
}
doLogThrottle(depth, logLvl, limit, format, fmt.Sprintf(format, v...))
}
func doLogThrottle(depth, logLvl, limit int, key, msg string) {
if LogLevel < logLvl {
return
}
throttleMu.Lock()
th, ok := logThrottles[key]
if !ok {
th = NewThrottler(limit, 3600*time.Second)
logThrottles[key] = th
}
skip, _ := th.Throttle()
throttleMu.Unlock()
if skip {
return
}
DoLog(depth, logLvl, msg)
}
// Logf to logger if setup
// Logf(ERROR, "message %d", 20)
func Logf(logLvl int, format string, v ...interface{}) {
if LogLevel >= logLvl {
DoLog(3, logLvl, fmt.Sprintf(format, v...))
}
}
func LogFieldsf(logLvl int, fields map[string]interface{}, format string, v ...interface{}) {
if LogLevel >= logLvl {
DoLogFields(3, logLvl, fmt.Sprintf(format, v...), fields)
}
}
// Log to logger if setup
// LogP(ERROR, "prefix", "message", anyItems, youWant)
func LogP(logLvl int, prefix string, v ...interface{}) {
if ErrLogLevel >= logLvl && loggerErr != nil {
loggerErr.Output(3, prefix+LogPrefix[logLvl]+fmt.Sprint(v...)+postFix)
} else if LogLevel >= logLvl && logger != nil {
logger.Output(3, prefix+LogPrefix[logLvl]+fmt.Sprint(v...)+postFix)
}
}
// Log to logger if setup with a prefix
// LogPf(ERROR, "prefix", "formatString %s %v", anyItems, youWant)
func LogPf(logLvl int, prefix string, format string, v ...interface{}) {
if ErrLogLevel >= logLvl && loggerErr != nil {
loggerErr.Output(3, prefix+LogPrefix[logLvl]+fmt.Sprintf(format, v...)+postFix)
} else if LogLevel >= logLvl && logger != nil {
logger.Output(3, prefix+LogPrefix[logLvl]+fmt.Sprintf(format, v...)+postFix)
}
}
// When you want to use the log short filename flag, and want to use
// the lower level logging functions (say from an *Assert* type function)
// you need to modify the stack depth:
//
// func init() {}
// SetLogger(log.New(os.Stderr, "", log.Ltime|log.Lshortfile|log.Lmicroseconds), lvl)
// }
//
// func assert(t *testing.T, myData) {
// // we want log line to show line that called this assert, not this line
// LogD(5, DEBUG, v...)
// }
func LogD(depth int, logLvl int, v ...interface{}) {
if LogLevel >= logLvl {
DoLog(depth, logLvl, fmt.Sprint(v...))
}
}
// Low level log with depth , level, message and logger
func DoLog(depth, logLvl int, msg string) {
DoLogFields(depth+1, logLvl, msg, nil)
}
// LogCtx Low level log with depth , level, message and logger
func LogCtx(ctx context.Context, depth, logLvl int, format string, v ...interface{}) {
if LogLevel >= logLvl {
msg := fmt.Sprintf(format, v...)
lc := FromContext(ctx)
if len(lc) > 0 {
msg = fmt.Sprintf("%s %s", lc, msg)
}
DoLog(depth+1, logLvl, msg)
}
}
// DoLogFields allows the inclusion of additional context for logrus logs
// file and line number are included in the fields by default
func DoLogFields(depth, logLvl int, msg string, fields map[string]interface{}) {
if escapeNewlines {
msg = EscapeNewlines(msg)
}
if customLogger == nil {
// Use standard logger
if ErrLogLevel >= logLvl && loggerErr != nil {
loggerErr.Output(depth, LogPrefix[logLvl]+msg+postFix)
} else if LogLevel >= logLvl && logger != nil {
logger.Output(depth, LogPrefix[logLvl]+msg+postFix)
}
} else {
customLogger.Log(depth, logLvl, msg, fields)
}
}
type winsize struct {
Row uint16
Col uint16
Xpixel uint16
Ypixel uint16
}
const (
_TIOCGWINSZ = 0x5413 // OSX 1074295912
)
// http://play.golang.org/p/5LIA41Iqfp
// Dummy discard, satisfies io.Writer without importing io or os.
type DevNull struct{}
func (DevNull) Write(p []byte) (int, error) {
return len(p), nil
}
// Replace standard newline characters with escaped newlines so long msgs will
// remain one line.
func EscapeNewlines(str string) string {
return strings.Replace(str, "\n", "\\n", -1)
}