Skip to content

Commit 932795f

Browse files
committed
log: make Fields type a generic map[string]any
Decouple it from logrus, but with the same type. Signed-off-by: Sebastiaan van Stijn <[email protected]> (cherry picked from commit 238da2c) Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 707ca94 commit 932795f

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

log/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var L = logrus.NewEntry(logrus.StandardLogger())
2929
type loggerKey struct{}
3030

3131
// Fields type to pass to "WithFields".
32-
type Fields = logrus.Fields
32+
type Fields = map[string]any
3333

3434
// Entry is a logging entry. It contains all the fields passed with
3535
// [Entry.WithFields]. It's finally logged when Trace, Debug, Info, Warn,

log/context_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"context"
2121
"reflect"
2222
"testing"
23+
24+
"github.com/sirupsen/logrus"
2325
)
2426

2527
func TestLoggerContext(t *testing.T) {
@@ -35,3 +37,27 @@ func TestLoggerContext(t *testing.T) {
3537
t.Errorf("should be the same: %+v, %+v", a, b)
3638
}
3739
}
40+
41+
func TestCompat(t *testing.T) {
42+
expected := Fields{
43+
"hello1": "world1",
44+
"hello2": "world2",
45+
"hello3": "world3",
46+
}
47+
48+
l := G(context.TODO())
49+
l = l.WithFields(logrus.Fields{"hello1": "world1"})
50+
l = l.WithFields(Fields{"hello2": "world2"})
51+
l = l.WithFields(map[string]any{"hello3": "world3"})
52+
if !reflect.DeepEqual(Fields(l.Data), expected) {
53+
t.Errorf("expected: (%[1]T) %+[1]v, got: (%[2]T) %+[2]v", expected, l.Data)
54+
}
55+
56+
l2 := L
57+
l2 = l2.WithFields(logrus.Fields{"hello1": "world1"})
58+
l2 = l2.WithFields(Fields{"hello2": "world2"})
59+
l2 = l2.WithFields(map[string]any{"hello3": "world3"})
60+
if !reflect.DeepEqual(Fields(l2.Data), expected) {
61+
t.Errorf("expected: (%[1]T) %+[1]v, got: (%[2]T) %+[2]v", expected, l2.Data)
62+
}
63+
}

0 commit comments

Comments
 (0)