diff --git a/internal/backends/hana/backend.go b/internal/backends/hana/backend.go index 933172df1073..98d10a64843b 100644 --- a/internal/backends/hana/backend.go +++ b/internal/backends/hana/backend.go @@ -20,7 +20,6 @@ import ( "log/slog" "github.com/prometheus/client_golang/prometheus" - "go.uber.org/zap" "github.com/FerretDB/FerretDB/internal/backends" "github.com/FerretDB/FerretDB/internal/util/fsql" @@ -51,7 +50,7 @@ func NewBackend(params *NewBackendParams) (backends.Backend, error) { return nil, err } - hdb := fsql.WrapDB(db, "hana", zap.L()) + hdb := fsql.WrapDB(db, "hana", params.L) hdb.BatchSize = params.BatchSize return backends.BackendContract(&backend{ diff --git a/internal/backends/mysql/metadata/pool/opendb.go b/internal/backends/mysql/metadata/pool/opendb.go index 1c7bb38a9c25..27158a01283f 100644 --- a/internal/backends/mysql/metadata/pool/opendb.go +++ b/internal/backends/mysql/metadata/pool/opendb.go @@ -21,7 +21,6 @@ import ( "time" _ "github.com/go-sql-driver/mysql" // register database/sql driver - "go.uber.org/zap" "github.com/FerretDB/FerretDB/internal/util/fsql" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" @@ -66,5 +65,5 @@ func openDB(uri string, l *slog.Logger, sp *state.Provider) (*fsql.DB, error) { } } - return fsql.WrapDB(db, "", zap.L()), nil + return fsql.WrapDB(db, "", l), nil } diff --git a/internal/backends/sqlite/metadata/pool/opendb.go b/internal/backends/sqlite/metadata/pool/opendb.go index 3b66ec465cb1..a9e1e3f6be68 100644 --- a/internal/backends/sqlite/metadata/pool/opendb.go +++ b/internal/backends/sqlite/metadata/pool/opendb.go @@ -19,7 +19,6 @@ import ( "database/sql" "log/slog" - "go.uber.org/zap" _ "modernc.org/sqlite" // register database/sql driver "github.com/FerretDB/FerretDB/internal/util/fsql" @@ -73,5 +72,5 @@ func openDB(name, uri string, memory bool, l *slog.Logger, sp *state.Provider) ( } } - return fsql.WrapDB(db, name, zap.L()), nil + return fsql.WrapDB(db, name, l), nil } diff --git a/internal/util/fsql/db.go b/internal/util/fsql/db.go index 2e3930f81a34..8bff340af196 100644 --- a/internal/util/fsql/db.go +++ b/internal/util/fsql/db.go @@ -18,12 +18,14 @@ package fsql import ( "context" "database/sql" + "fmt" + "log/slog" "time" "github.com/prometheus/client_golang/prometheus" - "go.uber.org/zap" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" + "github.com/FerretDB/FerretDB/internal/util/logging" "github.com/FerretDB/FerretDB/internal/util/resource" ) @@ -35,7 +37,7 @@ type DB struct { *metricsCollector sqlDB *sql.DB - l *zap.Logger + l *slog.Logger token *resource.Token BatchSize int } @@ -44,7 +46,7 @@ type DB struct { // // Name is used for metric label values, etc. // Logger (that will be named) is used for query logging. -func WrapDB(db *sql.DB, name string, l *zap.Logger) *DB { +func WrapDB(db *sql.DB, name string, l *slog.Logger) *DB { if db == nil { return nil } @@ -52,7 +54,7 @@ func WrapDB(db *sql.DB, name string, l *zap.Logger) *DB { res := &DB{ metricsCollector: newMetricsCollector(name, db.Stats), sqlDB: db, - l: l.Named(name), + l: logging.WithName(l, name), token: resource.NewToken(), } @@ -76,13 +78,13 @@ func (db *DB) Ping(ctx context.Context) error { func (db *DB) QueryContext(ctx context.Context, query string, args ...any) (*Rows, error) { start := time.Now() - fields := []any{zap.Any("args", args)} - db.l.Sugar().With(fields...).Debugf(">>> %s", query) + fields := []any{slog.Any("args", args)} + db.l.With(fields...).DebugContext(ctx, fmt.Sprintf(">>> %s", query)) rows, err := db.sqlDB.QueryContext(ctx, query, args...) - fields = append(fields, zap.Duration("time", time.Since(start)), zap.Error(err)) - db.l.Sugar().With(fields...).Debugf("<<< %s", query) + fields = append(fields, slog.Duration("time", time.Since(start)), logging.Error(err)) + db.l.With(fields...).DebugContext(ctx, fmt.Sprintf("<<< %s", query)) return wrapRows(rows), err } @@ -91,13 +93,13 @@ func (db *DB) QueryContext(ctx context.Context, query string, args ...any) (*Row func (db *DB) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row { start := time.Now() - fields := []any{zap.Any("args", args)} - db.l.Sugar().With(fields...).Debugf(">>> %s", query) + fields := []any{slog.Any("args", args)} + db.l.With(fields...).DebugContext(ctx, fmt.Sprintf(">>> %s", query)) row := db.sqlDB.QueryRowContext(ctx, query, args...) - fields = append(fields, zap.Duration("time", time.Since(start)), zap.Error(row.Err())) - db.l.Sugar().With(fields...).Debugf("<<< %s", query) + fields = append(fields, slog.Duration("time", time.Since(start)), logging.Error(row.Err())) + db.l.With(fields...).DebugContext(ctx, fmt.Sprintf("<<< %s", query)) return row } @@ -106,21 +108,18 @@ func (db *DB) QueryRowContext(ctx context.Context, query string, args ...any) *s func (db *DB) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) { start := time.Now() - fields := []any{zap.Any("args", args)} - db.l.Sugar().With(fields...).Debugf(">>> %s", query) + fields := []any{slog.Any("args", args)} + db.l.With(fields...).DebugContext(ctx, fmt.Sprintf(">>> %s", query)) res, err := db.sqlDB.ExecContext(ctx, query, args...) - // to differentiate between 0 and nil - var ra *int64 - if res != nil { - rav, _ := res.RowsAffected() - ra = &rav + ra, _ := res.RowsAffected() + fields = append(fields, slog.Int64("rows", ra)) } - fields = append(fields, zap.Int64p("rows", ra), zap.Duration("time", time.Since(start)), zap.Error(err)) - db.l.Sugar().With(fields...).Debugf("<<< %s", query) + fields = append(fields, slog.Duration("time", time.Since(start)), logging.Error(err)) + db.l.With(fields...).DebugContext(ctx, fmt.Sprintf("<<< %s", query)) return res, err } diff --git a/internal/util/fsql/tx.go b/internal/util/fsql/tx.go index 3da2f5d86874..bbb7ffb1fd5e 100644 --- a/internal/util/fsql/tx.go +++ b/internal/util/fsql/tx.go @@ -17,10 +17,11 @@ package fsql import ( "context" "database/sql" + "fmt" + "log/slog" "time" - "go.uber.org/zap" - + "github.com/FerretDB/FerretDB/internal/util/logging" "github.com/FerretDB/FerretDB/internal/util/resource" ) @@ -29,12 +30,12 @@ import ( // It exposes the subset of *sql.Tx methods we use. type Tx struct { sqlTx *sql.Tx - l *zap.Logger + l *slog.Logger token *resource.Token } // wrapTx creates new Tx. -func wrapTx(tx *sql.Tx, l *zap.Logger) *Tx { +func wrapTx(tx *sql.Tx, l *slog.Logger) *Tx { if tx == nil { return nil } @@ -66,13 +67,13 @@ func (tx *Tx) Rollback() error { func (tx *Tx) QueryContext(ctx context.Context, query string, args ...any) (*Rows, error) { start := time.Now() - fields := []any{zap.Any("args", args)} - tx.l.Sugar().With(fields...).Debugf(">>> %s", query) + fields := []any{slog.Any("args", args)} + tx.l.With(fields...).DebugContext(ctx, fmt.Sprintf(">>> %s", query)) rows, err := tx.sqlTx.QueryContext(ctx, query, args...) - fields = append(fields, zap.Duration("time", time.Since(start)), zap.Error(err)) - tx.l.Sugar().With(fields...).Debugf("<<< %s", query) + fields = append(fields, slog.Duration("time", time.Since(start)), logging.Error(err)) + tx.l.With(fields...).DebugContext(ctx, fmt.Sprintf("<<< %s", query)) return wrapRows(rows), err } @@ -81,13 +82,13 @@ func (tx *Tx) QueryContext(ctx context.Context, query string, args ...any) (*Row func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row { start := time.Now() - fields := []any{zap.Any("args", args)} - tx.l.Sugar().With(fields...).Debugf(">>> %s", query) + fields := []any{slog.Any("args", args)} + tx.l.With(fields...).DebugContext(ctx, fmt.Sprintf(">>> %s", query)) row := tx.sqlTx.QueryRowContext(ctx, query, args...) - fields = append(fields, zap.Duration("time", time.Since(start)), zap.Error(row.Err())) - tx.l.Sugar().With(fields...).Debugf("<<< %s", query) + fields = append(fields, slog.Duration("time", time.Since(start)), logging.Error(row.Err())) + tx.l.With(fields...).DebugContext(ctx, fmt.Sprintf("<<< %s", query)) return row } @@ -96,21 +97,18 @@ func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...any) *s func (tx *Tx) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) { start := time.Now() - fields := []any{zap.Any("args", args)} - tx.l.Sugar().With(fields...).Debugf(">>> %s", query) + fields := []any{slog.Any("args", args)} + tx.l.With(fields...).DebugContext(ctx, fmt.Sprintf(">>> %s", query)) res, err := tx.sqlTx.ExecContext(ctx, query, args...) - // to differentiate between 0 and nil - var ra *int64 - if res != nil { - rav, _ := res.RowsAffected() - ra = &rav + ra, _ := res.RowsAffected() + fields = append(fields, slog.Int64("rows", ra)) } - fields = append(fields, zap.Int64p("rows", ra), zap.Duration("time", time.Since(start)), zap.Error(err)) - tx.l.Sugar().With(fields...).Debugf("<<< %s", query) + fields = append(fields, slog.Duration("time", time.Since(start)), logging.Error(err)) + tx.l.With(fields...).DebugContext(ctx, fmt.Sprintf("<<< %s", query)) return res, err }