Skip to content

Commit 044ba47

Browse files
ursojackc
authored andcommitted
Add zerologadapter.NewContextLogger
This change introduces a new zerologadapter that allows users to pass the actual logger via context.Context. Especially HTTP middleware might choose to use `(*zerolog.Logger).WithContext` and `zerolog.Ctx`. Allowing users to extract the logger from the context keeps the full enriched logger available when pgx emits logs.
1 parent 0d20d12 commit 044ba47

File tree

2 files changed

+59
-12
lines changed

2 files changed

+59
-12
lines changed

log/zerologadapter/adapter.go

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import (
99
)
1010

1111
type Logger struct {
12-
logger zerolog.Logger
13-
withFunc func(context.Context, zerolog.Context) zerolog.Context
14-
skipModule bool
12+
logger zerolog.Logger
13+
withFunc func(context.Context, zerolog.Context) zerolog.Context
14+
fromContext bool
15+
skipModule bool
1516
}
1617

1718
// option options for configuring the logger when creating a new logger.
@@ -38,13 +39,28 @@ func NewLogger(logger zerolog.Logger, options ...option) *Logger {
3839
l := Logger{
3940
logger: logger,
4041
}
42+
l.init(options)
43+
return &l
44+
}
45+
46+
// NewContextLogger creates logger that extracts the zerolog.Logger from the
47+
// context.Context by using `zerolog.Ctx`. The zerolog.DefaultContextLogger will
48+
// be used if no logger is associated with the context.
49+
func NewContextLogger(options ...option) *Logger {
50+
l := Logger{
51+
fromContext: true,
52+
}
53+
l.init(options)
54+
return &l
55+
}
56+
57+
func (pl *Logger) init(options []option) {
4158
for _, opt := range options {
42-
opt(&l)
59+
opt(pl)
4360
}
44-
if !l.skipModule {
45-
l.logger = l.logger.With().Str("module", "pgx").Logger()
61+
if !pl.skipModule {
62+
pl.logger = pl.logger.With().Str("module", "pgx").Logger()
4663
}
47-
return &l
4864
}
4965

5066
func (pl *Logger) Log(ctx context.Context, level pgx.LogLevel, msg string, data map[string]interface{}) {
@@ -63,10 +79,24 @@ func (pl *Logger) Log(ctx context.Context, level pgx.LogLevel, msg string, data
6379
default:
6480
zlevel = zerolog.DebugLevel
6581
}
66-
zctx := pl.logger.With()
82+
83+
var zctx zerolog.Context
84+
if pl.fromContext {
85+
logger := zerolog.Ctx(ctx)
86+
zctx = logger.With()
87+
} else {
88+
zctx = pl.logger.With()
89+
}
6790
if pl.withFunc != nil {
6891
zctx = pl.withFunc(ctx, zctx)
6992
}
70-
pgxlog := zctx.Fields(data).Logger()
71-
pgxlog.WithLevel(zlevel).Msg(msg)
93+
94+
pgxlog := zctx.Logger()
95+
event := pgxlog.WithLevel(zlevel)
96+
if event.Enabled() {
97+
if pl.fromContext && !pl.skipModule {
98+
event.Str("module", "pgx")
99+
}
100+
event.Fields(data).Msg(msg)
101+
}
72102
}

log/zerologadapter/adapter_test.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,23 @@ func TestLogger(t *testing.T) {
3838
}
3939
})
4040

41+
t.Run("from context", func(t *testing.T) {
42+
var buf bytes.Buffer
43+
zlogger := zerolog.New(&buf)
44+
ctx := zlogger.WithContext(context.Background())
45+
logger := zerologadapter.NewContextLogger()
46+
logger.Log(ctx, pgx.LogLevelInfo, "hello", map[string]interface{}{"one": "two"})
47+
const want = `{"level":"info","module":"pgx","one":"two","message":"hello"}
48+
`
49+
50+
got := buf.String()
51+
if got != want {
52+
t.Log(got)
53+
t.Log(want)
54+
t.Errorf("%s != %s", got, want)
55+
}
56+
})
57+
4158
var buf bytes.Buffer
4259
type key string
4360
var ck key
@@ -52,7 +69,8 @@ func TestLogger(t *testing.T) {
5269
logWith = logWith.Str("req_id", id)
5370
}
5471
return logWith
55-
}))
72+
}),
73+
)
5674

5775
t.Run("no request id", func(t *testing.T) {
5876
buf.Reset()
@@ -77,5 +95,4 @@ func TestLogger(t *testing.T) {
7795
t.Errorf("%s != %s", got, want)
7896
}
7997
})
80-
8198
}

0 commit comments

Comments
 (0)