mirror of https://github.com/jackc/pgx.git
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.pull/1106/head
parent
0d20d1241e
commit
044ba47522
|
@ -9,9 +9,10 @@ import (
|
|||
)
|
||||
|
||||
type Logger struct {
|
||||
logger zerolog.Logger
|
||||
withFunc func(context.Context, zerolog.Context) zerolog.Context
|
||||
skipModule bool
|
||||
logger zerolog.Logger
|
||||
withFunc func(context.Context, zerolog.Context) zerolog.Context
|
||||
fromContext bool
|
||||
skipModule bool
|
||||
}
|
||||
|
||||
// option options for configuring the logger when creating a new logger.
|
||||
|
@ -38,15 +39,30 @@ func NewLogger(logger zerolog.Logger, options ...option) *Logger {
|
|||
l := Logger{
|
||||
logger: logger,
|
||||
}
|
||||
for _, opt := range options {
|
||||
opt(&l)
|
||||
}
|
||||
if !l.skipModule {
|
||||
l.logger = l.logger.With().Str("module", "pgx").Logger()
|
||||
}
|
||||
l.init(options)
|
||||
return &l
|
||||
}
|
||||
|
||||
// NewContextLogger creates logger that extracts the zerolog.Logger from the
|
||||
// context.Context by using `zerolog.Ctx`. The zerolog.DefaultContextLogger will
|
||||
// be used if no logger is associated with the context.
|
||||
func NewContextLogger(options ...option) *Logger {
|
||||
l := Logger{
|
||||
fromContext: true,
|
||||
}
|
||||
l.init(options)
|
||||
return &l
|
||||
}
|
||||
|
||||
func (pl *Logger) init(options []option) {
|
||||
for _, opt := range options {
|
||||
opt(pl)
|
||||
}
|
||||
if !pl.skipModule {
|
||||
pl.logger = pl.logger.With().Str("module", "pgx").Logger()
|
||||
}
|
||||
}
|
||||
|
||||
func (pl *Logger) Log(ctx context.Context, level pgx.LogLevel, msg string, data map[string]interface{}) {
|
||||
var zlevel zerolog.Level
|
||||
switch level {
|
||||
|
@ -63,10 +79,24 @@ func (pl *Logger) Log(ctx context.Context, level pgx.LogLevel, msg string, data
|
|||
default:
|
||||
zlevel = zerolog.DebugLevel
|
||||
}
|
||||
zctx := pl.logger.With()
|
||||
|
||||
var zctx zerolog.Context
|
||||
if pl.fromContext {
|
||||
logger := zerolog.Ctx(ctx)
|
||||
zctx = logger.With()
|
||||
} else {
|
||||
zctx = pl.logger.With()
|
||||
}
|
||||
if pl.withFunc != nil {
|
||||
zctx = pl.withFunc(ctx, zctx)
|
||||
}
|
||||
pgxlog := zctx.Fields(data).Logger()
|
||||
pgxlog.WithLevel(zlevel).Msg(msg)
|
||||
|
||||
pgxlog := zctx.Logger()
|
||||
event := pgxlog.WithLevel(zlevel)
|
||||
if event.Enabled() {
|
||||
if pl.fromContext && !pl.skipModule {
|
||||
event.Str("module", "pgx")
|
||||
}
|
||||
event.Fields(data).Msg(msg)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,6 +38,23 @@ func TestLogger(t *testing.T) {
|
|||
}
|
||||
})
|
||||
|
||||
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
|
||||
|
@ -52,7 +69,8 @@ func TestLogger(t *testing.T) {
|
|||
logWith = logWith.Str("req_id", id)
|
||||
}
|
||||
return logWith
|
||||
}))
|
||||
}),
|
||||
)
|
||||
|
||||
t.Run("no request id", func(t *testing.T) {
|
||||
buf.Reset()
|
||||
|
@ -77,5 +95,4 @@ func TestLogger(t *testing.T) {
|
|||
t.Errorf("%s != %s", got, want)
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue