From 930bb7a573f66594c94aca19d47ada08d66b64db Mon Sep 17 00:00:00 2001 From: ensiouel <ensiouel@gmail.com> Date: Thu, 1 Feb 2024 19:32:49 +0300 Subject: [PATCH] add custom fields --- pkg/log/zap/field.go | 56 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 pkg/log/zap/field.go diff --git a/pkg/log/zap/field.go b/pkg/log/zap/field.go new file mode 100644 index 00000000..77ddf563 --- /dev/null +++ b/pkg/log/zap/field.go @@ -0,0 +1,56 @@ +package zap + +import ( + "context" + "fmt" + + "go.uber.org/zap" + "go.uber.org/zap/zapcore" +) + +const unknownCaller = "unknown" + +func Category(category string) zapcore.Field { + return zap.String("category", category) +} + +func Component(component string) zapcore.Field { + return zap.String("component", component) +} + +func Event(event string) zapcore.Field { + return zap.String("event", event) +} + +func Object(v any) zapcore.Field { + switch object := v.(type) { + case string: + return zap.String("object", object) + case fmt.Stringer: + return zap.String("object", object.String()) + default: + return zap.Any("object", object) + } +} + +type callerKey struct{} + +func ContextWithCaller(parent context.Context, caller string) context.Context { + 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) +} + +func Attr(attr any) zapcore.Field { + return zap.Any("attr", attr) +} + +func Tags(tags ...string) zapcore.Field { + return zap.Strings("tags", tags) +} -- GitLab