1
0
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:
Arsen 2021-09-23 18:48:08 +03:00 committed by GitHub
parent af22cbb108
commit 3b60db0dcd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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