diff --git a/context.go b/context.go index ebd28f2..651f6fa 100644 --- a/context.go +++ b/context.go @@ -27,6 +27,19 @@ func fieldsFromContext(ctx context.Context) *contextFields { return v } +func FieldsFromContext(ctx context.Context) (fs map[string]interface{}) { + if cfs := fieldsFromContext(ctx); cfs != nil { + cfs.m.Lock() + fs = make(map[string]interface{}) + for k, v := range cfs.fs { + fs[k] = v + } + cfs.m.Unlock() + return + } + return +} + func ContextWithField(ctx context.Context, k string, v interface{}) context.Context { return ContextWithFields(ctx, map[string]interface{}{k: v}) } diff --git a/context_test.go b/context_test.go index a987d6c..98e086f 100644 --- a/context_test.go +++ b/context_test.go @@ -12,7 +12,7 @@ func TestContext(t *testing.T) { "k2": "v2", } ctx := ContextWithFields(context.Background(), fs) - if g := fieldsFromContext(ctx); !reflect.DeepEqual(fs, g.fs) { - t.Errorf("expected %+v, got %+v", fs, g.fs) + if g := FieldsFromContext(ctx); !reflect.DeepEqual(fs, g) { + t.Errorf("expected %+v, got %+v", fs, g) } }