...
1
16
17 package log
18
19 import (
20 "context"
21 "reflect"
22 "testing"
23
24 "github.com/sirupsen/logrus"
25 )
26
27 func TestLoggerContext(t *testing.T) {
28 const expected = "one"
29 ctx := context.Background()
30 ctx = WithLogger(ctx, G(ctx).WithField("test", expected))
31 if actual := GetLogger(ctx).Data["test"]; actual != expected {
32 t.Errorf("expected: %v, got: %v", expected, actual)
33 }
34 a := G(ctx)
35 b := GetLogger(ctx)
36 if !reflect.DeepEqual(a, b) || a != b {
37 t.Errorf("should be the same: %+v, %+v", a, b)
38 }
39 }
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 }
64
View as plain text