Skip to content

Commit 4025eb8

Browse files
committed
Fix 'field polution' on deferred logging
1 parent 1bd7492 commit 4025eb8

File tree

5 files changed

+299
-234
lines changed

5 files changed

+299
-234
lines changed

context/context.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ func New() (ctx *Context) {
4848
return
4949
}
5050

51+
func (ctx *Context) Copy() *Context {
52+
newContext := new(Context)
53+
newContext.Trace = ctx.Trace
54+
newContext.index = ctx.index
55+
newContext.level = ctx.level
56+
newContext.fields = make(map[string]interface{})
57+
58+
for k, v := range ctx.fields {
59+
newContext.fields[k] = v
60+
}
61+
62+
return newContext
63+
}
64+
5165
func (ctx *Context) Derived() (derived *Context) {
5266
ctx.index += 1
5367
derived = New()
@@ -56,8 +70,10 @@ func (ctx *Context) Derived() (derived *Context) {
5670
}
5771

5872
func (ctx *Context) Level(level int) *Context {
59-
ctx.level = convertLevel(level)
60-
return ctx
73+
newContext := ctx.Copy()
74+
newContext.level = convertLevel(level)
75+
76+
return newContext
6177
}
6278

6379
func (ctx *Context) Field(name string, value interface{}) *Context {

0 commit comments

Comments
 (0)