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

add custom fields

parent e0b4b8f3
Branches
Tags
No related merge requests found
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)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment