mirror of
https://github.com/linka-cloud/grpc.git
synced 2024-12-22 08:50:46 +00:00
logger: add WithContext support
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
parent
2bb79e6c11
commit
e70369a902
@ -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)}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user