-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontext_test.go
More file actions
95 lines (65 loc) · 2.63 KB
/
context_test.go
File metadata and controls
95 lines (65 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package logger_test
import (
"bytes"
"context"
"io"
"testing"
"github.com/hamba/logger/v2"
"github.com/hamba/logger/v2/ctx"
"github.com/stretchr/testify/assert"
)
func TestLogger_WithContextAttachesFields_Logfmt(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
log := logger.New(&buf, logger.LogfmtFormat(), logger.Info).With(ctx.Str("svc", "api"))
goCtx := logger.WithContext(context.Background(), log, ctx.Str("req_id", "abc123"))
log.FromContext(goCtx).Info("handled")
assert.Equal(t, "lvl=info msg=handled svc=api req_id=abc123\n", buf.String())
}
func TestLogger_WithContextAttachesFields_JSON(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
log := logger.New(&buf, logger.JSONFormat(), logger.Info).With(ctx.Str("svc", "api"))
goCtx := logger.WithContext(context.Background(), log, ctx.Str("req_id", "abc123"))
log.FromContext(goCtx).Info("handled")
assert.JSONEq(t, `{"lvl":"info","msg":"handled","svc":"api","req_id":"abc123"}`+"\n", buf.String())
}
func TestLogger_WithContextCanLayersFields(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
log := logger.New(&buf, logger.LogfmtFormat(), logger.Info)
goCtx := logger.WithContext(context.Background(), log, ctx.Str("req_id", "abc123"))
goCtx = logger.WithContext(goCtx, log, ctx.Str("user", "u456"))
log.FromContext(goCtx).Info("handled")
assert.Equal(t, "lvl=info msg=handled req_id=abc123 user=u456\n", buf.String())
}
func TestLogger_WithContextWithNoFieldsReturnsCtxUnchanged(t *testing.T) {
t.Parallel()
log := logger.New(io.Discard, logger.LogfmtFormat(), logger.Info)
goCtx := context.Background()
got := logger.WithContext(goCtx, log)
assert.Equal(t, goCtx, got)
}
func TestLogger_WithContextWithDiscardReturnsCtxUnchanged(t *testing.T) {
t.Parallel()
log := logger.New(io.Discard, logger.LogfmtFormat(), logger.Info)
goCtx := context.Background()
got := logger.WithContext(goCtx, log, ctx.Str("req_id", "abc123"))
assert.Equal(t, goCtx, got)
}
func TestLogger_FromContextWithNoContextFieldsReturnsSameLogger(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
log := logger.New(&buf, logger.LogfmtFormat(), logger.Info)
got := log.FromContext(context.Background())
assert.Same(t, log, got)
}
func TestLogger_FromContextWithDiscardReturnsSameLogger(t *testing.T) {
t.Parallel()
log := logger.New(io.Discard, logger.LogfmtFormat(), logger.Info)
goCtx := logger.WithContext(context.Background(), log, ctx.Str("req_id", "abc123"))
// Re-attach to a non-discard logger to prove the discard check fires first.
discardLog := logger.New(io.Discard, logger.LogfmtFormat(), logger.Info)
got := discardLog.FromContext(goCtx)
assert.Same(t, discardLog, got)
}