logger: add WithContext support

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
Adphi 2023-02-21 14:51:58 +01:00
parent 2bb79e6c11
commit e70369a902
Signed by: adphi
GPG Key ID: 46BE4062DB2397FF

View File

@ -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)}
}