mirror of
https://github.com/go-micro/go-micro.git
synced 2024-12-30 10:10:44 +02:00
logger helper: add "inject" method, to make a pair with "extract" (#2283)
This commit is contained in:
parent
af22cbb108
commit
3b60db0dcd
@ -14,8 +14,8 @@ func NewHelper(logger Logger) *Helper {
|
||||
}
|
||||
|
||||
// Extract always returns valid Helper with logger from context or with DefaultLogger as fallback.
|
||||
// Can be used in pair with function NewContext(ctx context.Context, l Logger) context.Context.
|
||||
// (e.g. propagate RequestID to logger in service handler methods).
|
||||
// Can be used in pair with function Inject.
|
||||
// Example: propagate RequestID to logger in service handler methods.
|
||||
func Extract(ctx context.Context) *Helper {
|
||||
if l, ok := FromContext(ctx); ok {
|
||||
return NewHelper(l)
|
||||
@ -24,6 +24,10 @@ func Extract(ctx context.Context) *Helper {
|
||||
return NewHelper(DefaultLogger)
|
||||
}
|
||||
|
||||
func (h *Helper) Inject(ctx context.Context) context.Context {
|
||||
return NewContext(ctx, h.logger)
|
||||
}
|
||||
|
||||
func (h *Helper) Log(level Level, args ...interface{}) {
|
||||
h.logger.Log(level, args...)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user