Use structured logging with slog (#475)
This commit is contained in:
38
db.go
38
db.go
@@ -11,7 +11,7 @@ import (
|
||||
"hash/crc64"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"log/slog"
|
||||
"math"
|
||||
"math/rand"
|
||||
"os"
|
||||
@@ -97,8 +97,8 @@ type DB struct {
|
||||
// Must be set before calling Open().
|
||||
Replicas []*Replica
|
||||
|
||||
// Where to send log messages, defaults to log.Default()
|
||||
Logger *log.Logger
|
||||
// Where to send log messages, defaults to global slog with databas epath.
|
||||
Logger *slog.Logger
|
||||
}
|
||||
|
||||
// NewDB returns a new instance of DB for a given path.
|
||||
@@ -114,8 +114,7 @@ func NewDB(path string) *DB {
|
||||
MaxCheckpointPageN: DefaultMaxCheckpointPageN,
|
||||
CheckpointInterval: DefaultCheckpointInterval,
|
||||
MonitorInterval: DefaultMonitorInterval,
|
||||
|
||||
Logger: log.Default(),
|
||||
Logger: slog.With("db", path),
|
||||
}
|
||||
|
||||
db.dbSizeGauge = dbSizeGaugeVec.WithLabelValues(db.path)
|
||||
@@ -461,7 +460,7 @@ func (db *DB) init() (err error) {
|
||||
|
||||
// If we have an existing shadow WAL, ensure the headers match.
|
||||
if err := db.verifyHeadersMatch(); err != nil {
|
||||
db.Logger.Printf("%s: init: cannot determine last wal position, clearing generation; %s", db.path, err)
|
||||
db.Logger.Warn("init: cannot determine last wal position, clearing generation", "error", err)
|
||||
if err := os.Remove(db.GenerationNamePath()); err != nil && !os.IsNotExist(err) {
|
||||
return fmt.Errorf("remove generation name: %w", err)
|
||||
}
|
||||
@@ -703,7 +702,7 @@ func (db *DB) Sync(ctx context.Context) (err error) {
|
||||
if err := db.init(); err != nil {
|
||||
return err
|
||||
} else if db.db == nil {
|
||||
Tracef("%s: sync: no database found", db.path)
|
||||
db.Logger.Debug("sync: no database found")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -729,7 +728,7 @@ func (db *DB) Sync(ctx context.Context) (err error) {
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot verify wal state: %w", err)
|
||||
}
|
||||
Tracef("%s: sync: info=%#v", db.path, info)
|
||||
db.Logger.Debug("sync", "info", &info)
|
||||
|
||||
// Track if anything in the shadow WAL changes and then notify at the end.
|
||||
changed := info.walSize != info.shadowWALSize || info.restart || info.reason != ""
|
||||
@@ -740,7 +739,7 @@ func (db *DB) Sync(ctx context.Context) (err error) {
|
||||
if info.generation, err = db.createGeneration(); err != nil {
|
||||
return fmt.Errorf("create generation: %w", err)
|
||||
}
|
||||
db.Logger.Printf("%s: sync: new generation %q, %s", db.path, info.generation, info.reason)
|
||||
db.Logger.Info("sync: new generation", "generation", info.generation, "reason", info.reason)
|
||||
|
||||
// Clear shadow wal info.
|
||||
info.shadowWALPath = db.ShadowWALPath(info.generation, 0)
|
||||
@@ -794,7 +793,7 @@ func (db *DB) Sync(ctx context.Context) (err error) {
|
||||
db.notify = make(chan struct{})
|
||||
}
|
||||
|
||||
Tracef("%s: sync: ok", db.path)
|
||||
db.Logger.Debug("sync: ok")
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -985,7 +984,8 @@ func (db *DB) initShadowWALFile(filename string) (int64, error) {
|
||||
}
|
||||
|
||||
func (db *DB) copyToShadowWAL(filename string) (newSize int64, err error) {
|
||||
Tracef("%s: copy-shadow: %s", db.path, filename)
|
||||
logger := db.Logger.With("filename", filename)
|
||||
logger.Debug("copy-shadow")
|
||||
|
||||
r, err := os.Open(db.WALPath())
|
||||
if err != nil {
|
||||
@@ -1049,7 +1049,7 @@ func (db *DB) copyToShadowWAL(filename string) (newSize int64, err error) {
|
||||
for {
|
||||
// Read next page from WAL file.
|
||||
if _, err := io.ReadFull(r, frame); err == io.EOF || err == io.ErrUnexpectedEOF {
|
||||
Tracef("%s: copy-shadow: break %s @ %d; err=%s", db.path, filename, offset, err)
|
||||
logger.Debug("copy-shadow: break", "offset", offset, "error", err)
|
||||
break // end of file or partial page
|
||||
} else if err != nil {
|
||||
return 0, fmt.Errorf("read wal: %w", err)
|
||||
@@ -1059,7 +1059,7 @@ func (db *DB) copyToShadowWAL(filename string) (newSize int64, err error) {
|
||||
salt0 := binary.BigEndian.Uint32(frame[8:])
|
||||
salt1 := binary.BigEndian.Uint32(frame[12:])
|
||||
if salt0 != hsalt0 || salt1 != hsalt1 {
|
||||
Tracef("%s: copy-shadow: break: salt mismatch", db.path)
|
||||
logger.Debug("copy-shadow: break: salt mismatch")
|
||||
break
|
||||
}
|
||||
|
||||
@@ -1069,7 +1069,7 @@ func (db *DB) copyToShadowWAL(filename string) (newSize int64, err error) {
|
||||
chksum0, chksum1 = Checksum(bo, chksum0, chksum1, frame[:8]) // frame header
|
||||
chksum0, chksum1 = Checksum(bo, chksum0, chksum1, frame[24:]) // frame data
|
||||
if chksum0 != fchksum0 || chksum1 != fchksum1 {
|
||||
Tracef("%s: copy shadow: checksum mismatch, skipping: offset=%d (%x,%x) != (%x,%x)", db.path, offset, chksum0, chksum1, fchksum0, fchksum1)
|
||||
logger.Debug("copy shadow: checksum mismatch, skipping", "offset", offset, "check", fmt.Sprintf("(%x,%x) != (%x,%x)", chksum0, chksum1, fchksum0, fchksum1))
|
||||
break
|
||||
}
|
||||
|
||||
@@ -1078,7 +1078,7 @@ func (db *DB) copyToShadowWAL(filename string) (newSize int64, err error) {
|
||||
return 0, fmt.Errorf("write temp shadow wal: %w", err)
|
||||
}
|
||||
|
||||
Tracef("%s: copy-shadow: ok %s offset=%d salt=%x %x", db.path, filename, offset, salt0, salt1)
|
||||
logger.Debug("copy-shadow: ok", "offset", offset, "salt", fmt.Sprintf("%x %x", salt0, salt1))
|
||||
offset += int64(len(frame))
|
||||
|
||||
// Update new size if written frame was a commit record.
|
||||
@@ -1391,7 +1391,7 @@ func (db *DB) execCheckpoint(mode string) (err error) {
|
||||
if err := db.db.QueryRow(rawsql).Scan(&row[0], &row[1], &row[2]); err != nil {
|
||||
return err
|
||||
}
|
||||
Tracef("%s: checkpoint: mode=%v (%d,%d,%d)", db.path, mode, row[0], row[1], row[2])
|
||||
db.Logger.Debug("checkpoint", "mode", mode, "result", fmt.Sprintf("%d,%d,%d", row[0], row[1], row[2]))
|
||||
|
||||
// Reacquire the read lock immediately after the checkpoint.
|
||||
if err := db.acquireReadLock(); err != nil {
|
||||
@@ -1416,7 +1416,7 @@ func (db *DB) monitor() {
|
||||
|
||||
// Sync the database to the shadow WAL.
|
||||
if err := db.Sync(db.ctx); err != nil && !errors.Is(err, context.Canceled) {
|
||||
db.Logger.Printf("%s: sync error: %s", db.path, err)
|
||||
db.Logger.Error("sync error", "error", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1560,10 +1560,6 @@ type RestoreOptions struct {
|
||||
|
||||
// Specifies how many WAL files are downloaded in parallel during restore.
|
||||
Parallelism int
|
||||
|
||||
// Logging settings.
|
||||
Logger *log.Logger
|
||||
Verbose bool
|
||||
}
|
||||
|
||||
// NewRestoreOptions returns a new instance of RestoreOptions with defaults.
|
||||
|
||||
Reference in New Issue
Block a user