Skip to content

Commit

Permalink
logutil: Fix unwanted mutation during WithComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Xie committed Oct 11, 2019
1 parent b8011da commit 88a6ba1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
3 changes: 1 addition & 2 deletions logutil/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ func WithComponent(e *logrus.Entry, component ...string) *logrus.Entry {
if c, ok := e.Data[ComponentKey].(string); ok {
component = append([]string{c}, component...)
}
e.Data[ComponentKey] = strings.Join(component, "::")
return e
return e.WithField(ComponentKey, strings.Join(component, "::"))
}
13 changes: 9 additions & 4 deletions logutil/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@ func TestWithComponent(t *testing.T) {
expect = "package.Struct1"
)

logutil.WithComponent(e, expect)
if c := e.Data[logutil.ComponentKey]; c != expect {
e2 := logutil.WithComponent(e, expect)
if c := e2.Data[logutil.ComponentKey]; c != expect {
t.Errorf("e.Data[logutil.ComponentKey] = %v, want %s", c, expect)
}

if e2.Data[logutil.ComponentKey] == e.Data[logutil.ComponentKey] {
t.Error("e2.Data[logutil.ComponentKey] == e.Data[logutil.ComponentKey], " +
"expected different values")
}

component := "package.Struct2"
expect = "package.Struct1::" + component
logutil.WithComponent(e, component)
if c := e.Data[logutil.ComponentKey]; c != expect {
e3 := logutil.WithComponent(e2, component)
if c := e3.Data[logutil.ComponentKey]; c != expect {
t.Errorf("e.Data[logutil.ComponentKey] = %v, want %s", c, expect)
}
}

0 comments on commit 88a6ba1

Please sign in to comment.