-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathadapter_test.go
118 lines (107 loc) · 3.23 KB
/
adapter_test.go
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package zerolog_test
import (
"bytes"
"context"
"testing"
"time"
zerologadapter "github.com/jackc/pgx-zerolog"
"github.com/jackc/pgx/v5/tracelog"
"github.com/rs/zerolog"
)
func TestLogger(t *testing.T) {
t.Run("default", func(t *testing.T) {
var buf bytes.Buffer
zlogger := zerolog.New(&buf)
logger := zerologadapter.NewLogger(zlogger)
logger.Log(context.Background(), tracelog.LogLevelInfo, "hello", map[string]interface{}{"one": "two"})
const want = `{"level":"info","module":"pgx","one":"two","message":"hello"}
`
got := buf.String()
if got != want {
t.Errorf("%s != %s", got, want)
}
})
t.Run("disable pgx module", func(t *testing.T) {
var buf bytes.Buffer
zlogger := zerolog.New(&buf)
logger := zerologadapter.NewLogger(zlogger, zerologadapter.WithoutPGXModule())
logger.Log(context.Background(), tracelog.LogLevelInfo, "hello", nil)
const want = `{"level":"info","message":"hello"}
`
got := buf.String()
if got != want {
t.Errorf("%s != %s", got, want)
}
})
t.Run("sub dictionary", func(t *testing.T) {
var buf bytes.Buffer
zlogger := zerolog.New(&buf).
With().
Str("time", "example with conflict key").
Logger()
logger := zerologadapter.NewLogger(zlogger, zerologadapter.WithSubDictionary("custom-pgx"))
data := map[string]interface{}{
"time": time.Millisecond.String(),
}
logger.Log(context.Background(), tracelog.LogLevelInfo, "hello", data)
const want = `{"level":"info","time":"example with conflict key","module":"pgx","custom-pgx":{"time":"1ms"},"message":"hello"}
`
got := buf.String()
if got != want {
t.Errorf("%s != %s", got, want)
}
})
t.Run("from context", func(t *testing.T) {
var buf bytes.Buffer
zlogger := zerolog.New(&buf)
ctx := zlogger.WithContext(context.Background())
logger := zerologadapter.NewContextLogger()
logger.Log(ctx, tracelog.LogLevelInfo, "hello", map[string]interface{}{"one": "two"})
const want = `{"level":"info","module":"pgx","one":"two","message":"hello"}
`
got := buf.String()
if got != want {
t.Log(got)
t.Log(want)
t.Errorf("%s != %s", got, want)
}
})
var buf bytes.Buffer
type key string
var ck key
zlogger := zerolog.New(&buf)
logger := zerologadapter.NewLogger(zlogger,
zerologadapter.WithContextFunc(func(ctx context.Context, logWith zerolog.Context) zerolog.Context {
// You can use zerolog.hlog.IDFromCtx(ctx) or even
// zerolog.log.Ctx(ctx) to fetch the whole logger instance from the
// context if you want.
id, ok := ctx.Value(ck).(string)
if ok {
logWith = logWith.Str("req_id", id)
}
return logWith
}),
)
t.Run("no request id", func(t *testing.T) {
buf.Reset()
ctx := context.Background()
logger.Log(ctx, tracelog.LogLevelInfo, "hello", nil)
const want = `{"level":"info","module":"pgx","message":"hello"}
`
got := buf.String()
if got != want {
t.Errorf("%s != %s", got, want)
}
})
t.Run("with request id", func(t *testing.T) {
buf.Reset()
ctx := context.WithValue(context.Background(), ck, "1")
logger.Log(ctx, tracelog.LogLevelInfo, "hello", map[string]interface{}{"two": "2"})
const want = `{"level":"info","module":"pgx","req_id":"1","two":"2","message":"hello"}
`
got := buf.String()
if got != want {
t.Errorf("%s != %s", got, want)
}
})
}