diff options
Diffstat (limited to 'internal/log')
-rw-r--r-- | internal/log/entry.go | 51 | ||||
-rw-r--r-- | internal/log/log.go | 92 | ||||
-rw-r--r-- | internal/log/syslog_test.go | 6 |
3 files changed, 88 insertions, 61 deletions
diff --git a/internal/log/entry.go b/internal/log/entry.go index 0d4e16e2b..d782be9f7 100644 --- a/internal/log/entry.go +++ b/internal/log/entry.go @@ -19,6 +19,7 @@ package log import ( + "context" "fmt" "syscall" @@ -27,91 +28,97 @@ import ( ) type Entry struct { - fields []kv.Field + ctx context.Context + kvs []kv.Field +} + +func (e Entry) WithContext(ctx context.Context) Entry { + e.ctx = ctx + return e } func (e Entry) WithField(key string, value interface{}) Entry { - e.fields = append(e.fields, kv.Field{K: key, V: value}) + e.kvs = append(e.kvs, kv.Field{K: key, V: value}) return e } -func (e Entry) WithFields(fields ...kv.Field) Entry { - e.fields = append(e.fields, fields...) +func (e Entry) WithFields(kvs ...kv.Field) Entry { + e.kvs = append(e.kvs, kvs...) return e } func (e Entry) Trace(a ...interface{}) { - logf(3, level.TRACE, e.fields, args(len(a)), a...) + logf(e.ctx, 3, level.TRACE, e.kvs, args(len(a)), a...) } func (e Entry) Tracef(s string, a ...interface{}) { - logf(3, level.TRACE, e.fields, s, a...) + logf(e.ctx, 3, level.TRACE, e.kvs, s, a...) } func (e Entry) Debug(a ...interface{}) { - logf(3, level.DEBUG, e.fields, args(len(a)), a...) + logf(e.ctx, 3, level.DEBUG, e.kvs, args(len(a)), a...) } func (e Entry) Debugf(s string, a ...interface{}) { - logf(3, level.DEBUG, e.fields, s, a...) + logf(e.ctx, 3, level.DEBUG, e.kvs, s, a...) } func (e Entry) Info(a ...interface{}) { - logf(3, level.INFO, e.fields, args(len(a)), a...) + logf(e.ctx, 3, level.INFO, e.kvs, args(len(a)), a...) } func (e Entry) Infof(s string, a ...interface{}) { - logf(3, level.INFO, e.fields, s, a...) + logf(e.ctx, 3, level.INFO, e.kvs, s, a...) } func (e Entry) Warn(a ...interface{}) { - logf(3, level.WARN, e.fields, args(len(a)), a...) + logf(e.ctx, 3, level.WARN, e.kvs, args(len(a)), a...) } func (e Entry) Warnf(s string, a ...interface{}) { - logf(3, level.WARN, e.fields, s, a...) + logf(e.ctx, 3, level.WARN, e.kvs, s, a...) } func (e Entry) Error(a ...interface{}) { - logf(3, level.ERROR, e.fields, args(len(a)), a...) + logf(e.ctx, 3, level.ERROR, e.kvs, args(len(a)), a...) } func (e Entry) Errorf(s string, a ...interface{}) { - logf(3, level.ERROR, e.fields, s, a...) + logf(e.ctx, 3, level.ERROR, e.kvs, s, a...) } func (e Entry) Fatal(a ...interface{}) { defer syscall.Exit(1) - logf(3, level.FATAL, e.fields, args(len(a)), a...) + logf(e.ctx, 3, level.FATAL, e.kvs, args(len(a)), a...) } func (e Entry) Fatalf(s string, a ...interface{}) { defer syscall.Exit(1) - logf(3, level.FATAL, e.fields, s, a...) + logf(e.ctx, 3, level.FATAL, e.kvs, s, a...) } func (e Entry) Panic(a ...interface{}) { defer panic(fmt.Sprint(a...)) - logf(3, level.PANIC, e.fields, args(len(a)), a...) + logf(e.ctx, 3, level.PANIC, e.kvs, args(len(a)), a...) } func (e Entry) Panicf(s string, a ...interface{}) { defer panic(fmt.Sprintf(s, a...)) - logf(3, level.PANIC, e.fields, s, a...) + logf(e.ctx, 3, level.PANIC, e.kvs, s, a...) } func (e Entry) Log(lvl level.LEVEL, a ...interface{}) { - logf(3, lvl, e.fields, args(len(a)), a...) + logf(e.ctx, 3, lvl, e.kvs, args(len(a)), a...) } func (e Entry) Logf(lvl level.LEVEL, s string, a ...interface{}) { - logf(3, lvl, e.fields, s, a...) + logf(e.ctx, 3, lvl, e.kvs, s, a...) } func (e Entry) Print(a ...interface{}) { - printf(3, e.fields, args(len(a)), a...) + printf(3, e.kvs, args(len(a)), a...) } func (e Entry) Printf(s string, a ...interface{}) { - printf(3, e.fields, s, a...) + printf(3, e.kvs, s, a...) } diff --git a/internal/log/log.go b/internal/log/log.go index 78fb143b6..9240d1e79 100644 --- a/internal/log/log.go +++ b/internal/log/log.go @@ -19,6 +19,7 @@ package log import ( + "context" "fmt" "log/syslog" "os" @@ -38,13 +39,21 @@ var ( // lvlstrs is the lookup table of log levels to strings. lvlstrs = level.Default() - // Syslog output, only set if enabled. + // syslog output, only set if enabled. sysout *syslog.Writer // timefmt is the logging time format used. timefmt = "02/01/2006 15:04:05.000" + + // ctxhooks allows modifying log content based on context. + ctxhooks []func(context.Context, []kv.Field) []kv.Field ) +// Hook adds the given hook to the global logger context hooks stack. +func Hook(hook func(ctx context.Context, kvs []kv.Field) []kv.Field) { + ctxhooks = append(ctxhooks, hook) +} + // Level returns the currently set log level. func Level() level.LEVEL { return level.LEVEL(loglvl.Load()) @@ -60,82 +69,86 @@ func New() Entry { return Entry{} } +func WithContext(ctx context.Context) Entry { + return Entry{ctx: ctx} +} + func WithField(key string, value interface{}) Entry { - return Entry{fields: []kv.Field{{K: key, V: value}}} + return New().WithField(key, value) } func WithFields(fields ...kv.Field) Entry { - return Entry{fields: fields} + return New().WithFields(fields...) } -func Trace(a ...interface{}) { - logf(3, level.TRACE, nil, args(len(a)), a...) +func Trace(ctx context.Context, a ...interface{}) { + logf(ctx, 3, level.TRACE, nil, args(len(a)), a...) } -func Tracef(s string, a ...interface{}) { - logf(3, level.TRACE, nil, s, a...) +func Tracef(ctx context.Context, s string, a ...interface{}) { + logf(ctx, 3, level.TRACE, nil, s, a...) } -func Debug(a ...interface{}) { - logf(3, level.DEBUG, nil, args(len(a)), a...) +func Debug(ctx context.Context, a ...interface{}) { + logf(ctx, 3, level.DEBUG, nil, args(len(a)), a...) } -func Debugf(s string, a ...interface{}) { - logf(3, level.DEBUG, nil, s, a...) +func Debugf(ctx context.Context, s string, a ...interface{}) { + logf(ctx, 3, level.DEBUG, nil, s, a...) } -func Info(a ...interface{}) { - logf(3, level.INFO, nil, args(len(a)), a...) +func Info(ctx context.Context, a ...interface{}) { + logf(ctx, 3, level.INFO, nil, args(len(a)), a...) } -func Infof(s string, a ...interface{}) { - logf(3, level.INFO, nil, s, a...) +func Infof(ctx context.Context, s string, a ...interface{}) { + logf(ctx, 3, level.INFO, nil, s, a...) } -func Warn(a ...interface{}) { - logf(3, level.WARN, nil, args(len(a)), a...) +func Warn(ctx context.Context, a ...interface{}) { + logf(ctx, 3, level.WARN, nil, args(len(a)), a...) } -func Warnf(s string, a ...interface{}) { - logf(3, level.WARN, nil, s, a...) +func Warnf(ctx context.Context, s string, a ...interface{}) { + logf(ctx, 3, level.WARN, nil, s, a...) } -func Error(a ...interface{}) { - logf(3, level.ERROR, nil, args(len(a)), a...) +func Error(ctx context.Context, a ...interface{}) { + logf(ctx, 3, level.ERROR, nil, args(len(a)), a...) } -func Errorf(s string, a ...interface{}) { - logf(3, level.ERROR, nil, s, a...) +func Errorf(ctx context.Context, s string, a ...interface{}) { + logf(ctx, 3, level.ERROR, nil, s, a...) } -func Fatal(a ...interface{}) { +func Fatal(ctx context.Context, a ...interface{}) { defer syscall.Exit(1) - logf(3, level.FATAL, nil, args(len(a)), a...) + logf(ctx, 3, level.FATAL, nil, args(len(a)), a...) } -func Fatalf(s string, a ...interface{}) { +func Fatalf(ctx context.Context, s string, a ...interface{}) { defer syscall.Exit(1) - logf(3, level.FATAL, nil, s, a...) + logf(ctx, 3, level.FATAL, nil, s, a...) } -func Panic(a ...interface{}) { +func Panic(ctx context.Context, a ...interface{}) { defer panic(fmt.Sprint(a...)) - logf(3, level.PANIC, nil, args(len(a)), a...) + logf(ctx, 3, level.PANIC, nil, args(len(a)), a...) } -func Panicf(s string, a ...interface{}) { +func Panicf(ctx context.Context, s string, a ...interface{}) { defer panic(fmt.Sprintf(s, a...)) - logf(3, level.PANIC, nil, s, a...) + logf(ctx, 3, level.PANIC, nil, s, a...) } // Log will log formatted args as 'msg' field to the log at given level. -func Log(lvl level.LEVEL, a ...interface{}) { - logf(3, lvl, nil, args(len(a)), a...) +func Log(ctx context.Context, lvl level.LEVEL, a ...interface{}) { + logf(ctx, 3, lvl, nil, args(len(a)), a...) } // Logf will log format string as 'msg' field to the log at given level. -func Logf(lvl level.LEVEL, s string, a ...interface{}) { - logf(3, lvl, nil, s, a...) +func Logf(ctx context.Context, lvl level.LEVEL, s string, a ...interface{}) { + logf(ctx, 3, lvl, nil, s, a...) } // Print will log formatted args to the stdout log output. @@ -186,7 +199,7 @@ func printf(depth int, fields []kv.Field, s string, a ...interface{}) { putBuf(buf) } -func logf(depth int, lvl level.LEVEL, fields []kv.Field, s string, a ...interface{}) { +func logf(ctx context.Context, depth int, lvl level.LEVEL, fields []kv.Field, s string, a ...interface{}) { var out *os.File // Check if enabled. @@ -220,6 +233,13 @@ func logf(depth int, lvl level.LEVEL, fields []kv.Field, s string, a ...interfac buf.B = append(buf.B, lvlstrs[lvl]...) buf.B = append(buf.B, ' ') + if ctx != nil { + // Pass context through hooks. + for _, hook := range ctxhooks { + fields = hook(ctx, fields) + } + } + // Append formatted fields with msg kv.Fields(append(fields, kv.Field{ K: "msg", V: fmt.Sprintf(s, a...), diff --git a/internal/log/syslog_test.go b/internal/log/syslog_test.go index baa4dac6c..0fb46923c 100644 --- a/internal/log/syslog_test.go +++ b/internal/log/syslog_test.go @@ -66,14 +66,14 @@ func (suite *SyslogTestSuite) TearDownTest() { } func (suite *SyslogTestSuite) TestSyslog() { - log.Info("this is a test of the emergency broadcast system!") + log.Info(nil, "this is a test of the emergency broadcast system!") entry := <-suite.syslogChannel suite.Regexp(regexp.MustCompile(`timestamp=.* func=.* level=INFO msg="this is a test of the emergency broadcast system!"`), entry["content"]) } func (suite *SyslogTestSuite) TestSyslogLongMessage() { - log.Warn(longMessage) + log.Warn(nil, longMessage) funcName := log.Caller(2) prefix := fmt.Sprintf(`timestamp="02/01/2006 15:04:05.000" func=%s level=WARN msg="`, funcName) @@ -104,7 +104,7 @@ func (suite *SyslogTestSuite) TestSyslogLongMessageUnixgram() { testrig.InitTestLog() - log.Warn(longMessage) + log.Warn(nil, longMessage) funcName := log.Caller(2) prefix := fmt.Sprintf(`timestamp="02/01/2006 15:04:05.000" func=%s level=WARN msg="`, funcName) |