Skip to content
Snippets Groups Projects
Commit bcfc1646 authored by ensiouel's avatar ensiouel
Browse files

refactor

parent fa2242c6
No related branches found
No related tags found
No related merge requests found
...@@ -43,16 +43,12 @@ func Object(v any) zapcore.Field { ...@@ -43,16 +43,12 @@ func Object(v any) zapcore.Field {
type callerKey struct{} type callerKey struct{}
// ContextWithCaller добавляет передаваемый аргумент в качестве "вызывающего" в формате GlobalID. // ContextWithCaller добавляет в контекст передаваемый аргумент в качестве "вызывающего" в формате GlobalID.
// Поддерживает один из типов: string, fmt.Stringer. Если передан аргумент другого типа, в качестве вызывающего // Поддерживает один из типов: string, fmt.Stringer. Если передан аргумент другого типа, в качестве вызывающего
// будет установлен "unknown". // будет установлен "unknown".
func ContextWithCaller(parent context.Context, caller any) context.Context { func ContextWithCaller(parent context.Context, v any) context.Context {
return context.WithValue(parent, callerKey{}, caller)
}
func CallerFromContext(ctx context.Context) zapcore.Field {
var caller string var caller string
switch value := ctx.Value(callerKey{}).(type) { switch value := v.(type) {
case string: case string:
caller = value caller = value
case fmt.Stringer: case fmt.Stringer:
...@@ -60,6 +56,14 @@ func CallerFromContext(ctx context.Context) zapcore.Field { ...@@ -60,6 +56,14 @@ func CallerFromContext(ctx context.Context) zapcore.Field {
default: default:
caller = unknownCaller 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) return zap.String("caller", caller)
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment