Skip to content

Commit 315ddd9

Browse files
authored
logging/zap/ctxzap: add shorthand functions (#408)
Fixes #407
1 parent a77ba4d commit 315ddd9

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

logging/zap/ctxzap/context.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,27 @@ func ToContext(ctx context.Context, logger *zap.Logger) context.Context {
6262
}
6363
return context.WithValue(ctx, ctxMarkerKey, l)
6464
}
65+
66+
// Debug is equivalent to calling Debug on the zap.Logger in the context.
67+
// It is a no-op if the context does not contain a zap.Logger.
68+
func Debug(ctx context.Context, msg string, fields ...zap.Field) {
69+
Extract(ctx).Debug(msg, fields...)
70+
}
71+
72+
// Info is equivalent to calling Info on the zap.Logger in the context.
73+
// It is a no-op if the context does not contain a zap.Logger.
74+
func Info(ctx context.Context, msg string, fields ...zap.Field) {
75+
Extract(ctx).Info(msg, fields...)
76+
}
77+
78+
// Warn is equivalent to calling Warn on the zap.Logger in the context.
79+
// It is a no-op if the context does not contain a zap.Logger.
80+
func Warn(ctx context.Context, msg string, fields ...zap.Field) {
81+
Extract(ctx).Warn(msg, fields...)
82+
}
83+
84+
// Error is equivalent to calling Error on the zap.Logger in the context.
85+
// It is a no-op if the context does not contain a zap.Logger.
86+
func Error(ctx context.Context, msg string, fields ...zap.Field) {
87+
Extract(ctx).Error(msg, fields...)
88+
}

logging/zap/ctxzap/context_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package ctxzap
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"go.uber.org/zap"
8+
"go.uber.org/zap/zapcore"
9+
"go.uber.org/zap/zaptest"
10+
)
11+
12+
func TestShorthands(t *testing.T) {
13+
cases := []struct {
14+
fn func(ctx context.Context, msg string, fields ...zapcore.Field)
15+
level zapcore.Level
16+
}{
17+
{Debug, zap.DebugLevel},
18+
{Info, zap.InfoLevel},
19+
{Warn, zap.WarnLevel},
20+
{Error, zap.ErrorLevel},
21+
}
22+
const message = "omg!"
23+
for _, c := range cases {
24+
t.Run(c.level.String(), func(t *testing.T) {
25+
called := false
26+
logger := zaptest.NewLogger(t, zaptest.WrapOptions(zap.Hooks(func(e zapcore.Entry) error {
27+
called = true
28+
if e.Level != c.level {
29+
t.Fatalf("Expected %v, got %v", c.level, e.Level)
30+
}
31+
if e.Message != message {
32+
t.Fatalf("message: expected %v, got %v", message, e.Message)
33+
}
34+
return nil
35+
})))
36+
ctx := ToContext(context.Background(), logger)
37+
c.fn(ctx, message)
38+
if !called {
39+
t.Fatal("hook not called")
40+
}
41+
})
42+
}
43+
}
44+
45+
func TestShorthandsNoop(t *testing.T) {
46+
// Just check we don't panic if there is no logger in the context.
47+
Debug(context.Background(), "no-op")
48+
Info(context.Background(), "no-op")
49+
Warn(context.Background(), "no-op")
50+
Error(context.Background(), "no-op")
51+
}

0 commit comments

Comments
 (0)