diff --git a/pkg/log/zap/field.go b/pkg/log/zap/field.go index 46dabda128d28050572997c6dff3c4514dc3555c..4cf6ad44acf9f141e9fc5863790467fd9a4cdeb4 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) }