Skip to content

Commit

Permalink
Map pgx.LogLevelInfo to zapcore.DebugLevel, hide password on panic (#3)
Browse files Browse the repository at this point in the history
* Map pgx.LogLevelInfo to zapcore.DebugLevel, hide password on panic

* Secure password

Co-authored-by: Alexey Ankip <[email protected]>
  • Loading branch information
unkeep and Alexey Ankip authored Jan 14, 2021
1 parent 5e823a0 commit ff536e7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
10 changes: 6 additions & 4 deletions pgx_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"
"fmt"
"net"
"strings"
"time"

"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/log/zapadapter"
"github.com/jackc/pgx/v4/pgxpool"
)

Expand Down Expand Up @@ -77,7 +77,7 @@ func NewClient(ctx context.Context, cfg Config) Client {

poolCfg, err := pgxpool.ParseConfig(cfg.ConnString)
if err != nil {
panic(fmt.Errorf("connect to postgres %s: %v", cfg.ConnString, err))
panic(fmt.Errorf("parse config: %v", err))
}

if cfg.TCPKeepAlivePeriod == 0 {
Expand All @@ -91,15 +91,17 @@ func NewClient(ctx context.Context, cfg Config) Client {
poolCfg.ConnConfig.DialFunc = dialer.DialContext
poolCfg.MaxConns = cfg.MaxConnections
if cfg.Logger != nil {
poolCfg.ConnConfig.Logger = zapadapter.NewLogger(cfg.Logger)
poolCfg.ConnConfig.Logger = newLoggerAdapter(cfg.Logger)
}
poolCfg.BeforeAcquire = func(ctx context.Context, conn *pgx.Conn) bool {
return !conn.IsClosed()
}

connPool, err := pgxpool.ConnectConfig(ctx, poolCfg)
if err != nil {
panic(fmt.Errorf("connect to postgres %s: %v", cfg.ConnString, err))
msg := fmt.Sprintf("connect to postgres %q: %v", cfg.ConnString, err)
msg = strings.ReplaceAll(msg, poolCfg.ConnConfig.Password, "*****")
panic(msg)
}

if err := collector.register(cfg.Name, connPool); err != nil {
Expand Down
44 changes: 44 additions & 0 deletions pgx_logger_adapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package pq

import (
"context"

"github.com/jackc/pgx/v4"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

type loggerAdapter struct {
logger *zap.Logger
}

func newLoggerAdapter(logger *zap.Logger) *loggerAdapter {
return &loggerAdapter{logger: logger.WithOptions(zap.AddCallerSkip(1))}
}

func (a *loggerAdapter) Log(ctx context.Context, pgxLevel pgx.LogLevel, msg string, data map[string]interface{}) {
fields := make([]zapcore.Field, len(data)+1)
i := 0
for k, v := range data {
fields[i] = zap.Reflect(k, v)
i++
}
fields[i] = zap.Stringer("PGX_LOG_LEVEL", pgxLevel)

level := a.level(pgxLevel)

a.logger.Check(level, msg).Write(fields...)
}

func (a *loggerAdapter) level(level pgx.LogLevel) zapcore.Level {
switch level {
case pgx.LogLevelTrace, pgx.LogLevelDebug, pgx.LogLevelInfo:
return zapcore.DebugLevel
case pgx.LogLevelWarn:
return zapcore.WarnLevel
case pgx.LogLevelError:
return zapcore.ErrorLevel
default:
return zapcore.ErrorLevel
}
}

0 comments on commit ff536e7

Please sign in to comment.