Skip to content

Commit 43a1474

Browse files
authored
add support to trace on grpc_logrus.DefaultMessageProducer (#547)
* add unit test * update docs after `make all` * refactor unit test, refactor option * Revert "update docs after `make all`" This reverts commit 10f2e4c.
1 parent 18e890f commit 43a1474

File tree

2 files changed

+79
-15
lines changed

2 files changed

+79
-15
lines changed

logging/logrus/options.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -212,19 +212,6 @@ func DefaultMessageProducer(ctx context.Context, format string, level logrus.Lev
212212
if err != nil {
213213
fields[logrus.ErrorKey] = err
214214
}
215-
entry := ctxlogrus.Extract(ctx).WithContext(ctx).WithFields(fields)
216-
switch level {
217-
case logrus.DebugLevel:
218-
entry.Debugf(format)
219-
case logrus.InfoLevel:
220-
entry.Infof(format)
221-
case logrus.WarnLevel:
222-
entry.Warningf(format)
223-
case logrus.ErrorLevel:
224-
entry.Errorf(format)
225-
case logrus.FatalLevel:
226-
entry.Fatalf(format)
227-
case logrus.PanicLevel:
228-
entry.Panicf(format)
229-
}
215+
216+
ctxlogrus.Extract(ctx).WithContext(ctx).WithFields(fields).Logf(level, format)
230217
}

logging/logrus/options_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,90 @@
11
package grpc_logrus
22

33
import (
4+
"context"
5+
"errors"
46
"testing"
57
"time"
68

9+
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
10+
"github.com/sirupsen/logrus"
11+
"github.com/sirupsen/logrus/hooks/test"
712
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
14+
"google.golang.org/grpc/codes"
815
)
916

1017
func TestDurationToTimeMillisField(t *testing.T) {
1118
_, val := DurationToTimeMillisField(time.Microsecond * 100)
1219
assert.Equal(t, val.(float32), float32(0.1), "sub millisecond values should be correct")
1320
}
21+
22+
func TestDefaultMessageProducer(t *testing.T) {
23+
t.Parallel()
24+
25+
errNotFound := errors.New("not found")
26+
testcases := []struct {
27+
label string
28+
format string
29+
level logrus.Level
30+
code codes.Code
31+
err error
32+
fields logrus.Fields
33+
}{
34+
{
35+
label: "should emit info message without error",
36+
format: "test",
37+
level: logrus.InfoLevel,
38+
code: codes.OK,
39+
fields: logrus.Fields{
40+
"foo": "bar",
41+
},
42+
},
43+
{
44+
label: "should emit info message with error",
45+
format: "test",
46+
level: logrus.InfoLevel,
47+
code: codes.NotFound,
48+
err: errNotFound,
49+
fields: logrus.Fields{
50+
"foo": "bar",
51+
logrus.ErrorKey: errNotFound,
52+
},
53+
},
54+
{
55+
label: "should emit trace message without error",
56+
format: "test",
57+
level: logrus.TraceLevel,
58+
code: codes.OK,
59+
fields: logrus.Fields{
60+
"foo": "bar",
61+
},
62+
},
63+
}
64+
65+
for _, tc := range testcases {
66+
tc := tc
67+
t.Run(tc.label, func(t *testing.T) {
68+
t.Parallel()
69+
70+
logger, hook := test.NewNullLogger()
71+
72+
logger.SetLevel(logrus.TraceLevel)
73+
74+
logrusEntry := logger.WithField("foo", "bar")
75+
76+
ctx := ctxlogrus.ToContext(context.Background(), logrusEntry)
77+
78+
DefaultMessageProducer(ctx, tc.format, tc.level, tc.code, tc.err, logrus.Fields{})
79+
80+
lastEntry := hook.LastEntry()
81+
82+
require.NotNil(t, lastEntry)
83+
84+
assert.Equal(t, "test", lastEntry.Message)
85+
assert.Equal(t, tc.fields, lastEntry.Data)
86+
assert.Equal(t, tc.level, lastEntry.Level)
87+
})
88+
}
89+
90+
}

0 commit comments

Comments
 (0)