mirror of
https://github.com/linka-cloud/grpc.git
synced 2024-11-21 18:36:25 +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
|
package logger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
@ -26,6 +27,8 @@ func FromLogrus(fl logrus.Ext1FieldLogger) Logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Logger interface {
|
type Logger interface {
|
||||||
|
WithContext(ctx context.Context) Logger
|
||||||
|
|
||||||
WithField(key string, value interface{}) Logger
|
WithField(key string, value interface{}) Logger
|
||||||
WithFields(kv ...interface{}) Logger
|
WithFields(kv ...interface{}) Logger
|
||||||
WithError(err error) Logger
|
WithError(err error) Logger
|
||||||
@ -191,6 +194,16 @@ func (l *logger) SetLevel(level logrus.Level) Logger {
|
|||||||
return l
|
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 {
|
func (l *logger) WithField(key string, value interface{}) Logger {
|
||||||
return &logger{fl: l.fl.WithField(key, value)}
|
return &logger{fl: l.fl.WithField(key, value)}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user