Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions loggers/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ func (o *LogFields) Del(key string) {
// AddToLogContext adds log fields to context.
// Any info added here will be added to all logs using this context
func AddToLogContext(ctx context.Context, key string, value interface{}) context.Context {
data := FromContext(ctx)
if data == nil {
ctx = context.WithValue(ctx, contextKey, new(LogFields))
data = FromContext(ctx)
existingFields := FromContext(ctx)
newFields := &LogFields{}

if existingFields != nil {
existingFields.Range(func(key, value interface{}) bool {
newFields.Add(key.(string), value)
return true
})
}
if data != nil {
data.Add(key, value)
}
return ctx

newFields.Add(key, value)
return context.WithValue(ctx, contextKey, newFields)
}

// FromContext fetchs log fields from provided context
Expand Down