From e70369a902124b18952f1e03126039c3a7343045 Mon Sep 17 00:00:00 2001 From: Adphi Date: Tue, 21 Feb 2023 14:51:58 +0100 Subject: [PATCH] logger: add WithContext support Signed-off-by: Adphi --- logger/logger.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/logger/logger.go b/logger/logger.go index 77d2b4c..51a5666 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -1,6 +1,7 @@ package logger import ( + "context" "fmt" "io" @@ -26,6 +27,8 @@ func FromLogrus(fl logrus.Ext1FieldLogger) Logger { } type Logger interface { + WithContext(ctx context.Context) Logger + WithField(key string, value interface{}) Logger WithFields(kv ...interface{}) Logger WithError(err error) Logger @@ -191,6 +194,16 @@ func (l *logger) SetLevel(level logrus.Level) Logger { return l } +func (l *logger) WithContext(ctx context.Context) Logger { + switch t := l.fl.(type) { + case *logrus.Logger: + return &logger{fl: t.WithContext(ctx)} + case *logrus.Entry: + return &logger{fl: t.WithContext(ctx)} + } + panic(fmt.Sprintf("unexpected logger type %T", l.fl)) +} + func (l *logger) WithField(key string, value interface{}) Logger { return &logger{fl: l.fl.WithField(key, value)} }