File: adapter_test.go

package info (click to toggle)
golang-github-jackc-pgx 4.18.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 796 kB
  • sloc: sh: 44; sql: 8; makefile: 7
file content (98 lines) | stat: -rw-r--r-- 2,640 bytes parent folder | download | duplicates (2)
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
package zerologadapter_test

import (
	"bytes"
	"context"
	"testing"

	"github.com/jackc/pgx/v4"
	"github.com/jackc/pgx/v4/log/zerologadapter"
	"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(), pgx.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(), pgx.LogLevelInfo, "hello", nil)
		const want = `{"level":"info","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, pgx.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, pgx.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, pgx.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)
		}
	})
}