From bcfc16465f17f30f4d38e5b74481096dac9e3cbf Mon Sep 17 00:00:00 2001 From: ensiouel <ensiouel@gmail.com> Date: Fri, 2 Feb 2024 09:42:55 +0300 Subject: [PATCH] refactor --- pkg/log/zap/field.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/pkg/log/zap/field.go b/pkg/log/zap/field.go index 46dabda1..4cf6ad44 100644 --- a/pkg/log/zap/field.go +++ b/pkg/log/zap/field.go @@ -43,16 +43,12 @@ func Object(v any) zapcore.Field { type callerKey struct{} -// ContextWithCaller добавляет передаваемый аргумент в качестве "вызывающего" в формате GlobalID. +// ContextWithCaller добавляет в контекст передаваемый аргумент в качестве "вызывающего" в формате GlobalID. // Поддерживает один из типов: string, fmt.Stringer. Если передан аргумент другого типа, в качестве вызывающего // будет установлен "unknown". -func ContextWithCaller(parent context.Context, caller any) context.Context { - return context.WithValue(parent, callerKey{}, caller) -} - -func CallerFromContext(ctx context.Context) zapcore.Field { +func ContextWithCaller(parent context.Context, v any) context.Context { var caller string - switch value := ctx.Value(callerKey{}).(type) { + switch value := v.(type) { case string: caller = value case fmt.Stringer: @@ -60,6 +56,14 @@ func CallerFromContext(ctx context.Context) zapcore.Field { default: caller = unknownCaller } + return context.WithValue(parent, callerKey{}, caller) +} + +func CallerFromContext(ctx context.Context) zapcore.Field { + caller, ok := ctx.Value(callerKey{}).(string) + if !ok { + caller = unknownCaller + } return zap.String("caller", caller) } -- GitLab