jackc / jackc/pgx

zap logger adapter should not do more work than neccessary

Open
#1,014 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
14.3k
Forks
1.1k
Avg merge
6d 9h
Merged PRs (30d)
11

Description

zap logger adapter does a lot of work massaging fields without even checking if a particular logging level is enabled. A more efficient implementation will look something like the following:

```go
func (pl *Logger) Log(ctx context.Context, level pgx.LogLevel, msg string, data map[string]interface{}) {
zapLevel := zapcore.DebugLevel
keepSourceLevel := false

switch level {
case pgx.LogLevelInfo:
zapLevel = zapcore.InfoLevel
case pgx.LogLevelWarn:
zapLevel = zapcore.WarnLevel
case pgx.LogLevelError:
zapLevel = zapcore.ErrorLevel
default:
keepSourceLevel = true
}

ce := pl.logger.Check(zapLevel, msg)
if ce == nil {
return
}

fields := make([]zapcore.Field, 0, len(data) + 1)
for k, v := range data {
fields = append(fields, zap.Any(k, v))
}

if keepSourceLevel {
fields = append(fields, zap.Stringer("PGX_LOG_LEVEL", level))
}

ce.Write(fields...)
}
```

Contributor guide

Open the contributing guide

Research direction

Locate the zap logger adapter and its Log method, then read the surrounding logging and test code to understand the current field handling. Done means disabled log levels avoid field processing while enabled levels preserve the existing message, fields, and source-level behavior; add or update tests for both paths.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
observability
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.