Logging: structured slog messages throughout, and WithLogger options so callers choose the logger

Log messages are lower-case event names with their details as key/value
attributes ("error" for errors), in place of formatted strings.

Loggable package take a WithLogger option as part of their "New*" call,
defaulting to slog.Default(). Each tags its lines with its `component`.

Some debug loggers print directly rather than using slog when in
terminal logging mode.
This commit is contained in:
bwilliams committed 2026-10-15 16:10:52 +01:00
1 parent 0b5fde9563
commit 043e307663
49 files changed
+598 -248

No files matched your search

+9 -6
View File
@@ -4,7 +4,6 @@ import (
"context"
"fmt"
"hash/fnv"
"log"
"math"
"math/rand"
"time"
@@ -21,15 +20,18 @@ type Generator struct {
cat *Catalog
store *Store
rnd *rand.Rand
opts options
}
// NewGenerator returns a generator drawing from cat and storing releases in
// store. The same seed and store contents give the same releases.
func NewGenerator(cat *Catalog, store *Store, seed int64) *Generator {
// store. The same seed and store contents give the same releases. It logs to
// slog.Default() unless given WithLogger.
func NewGenerator(cat *Catalog, store *Store, seed int64, opts ...Option) *Generator {
return &Generator{
cat: cat,
store: store,
rnd: rand.New(rand.NewSource(seed)), //nolint:gosec // seeded for repeatable mock data
opts: applyOptions(opts),
}
}
@@ -235,9 +237,10 @@ func (g *Generator) CatchUp(
// Run publishes a new release after a random gap between min and max until ctx ends.
func (g *Generator) Run(ctx context.Context, min, max time.Duration) {
logger := g.opts.logger("generator")
for {
d := g.gap(min, max)
log.Printf("generator: next release in %s", d.Round(time.Second))
logger.Info("next release", "in", d.Round(time.Second))
timer := time.NewTimer(d)
select {
@@ -249,11 +252,11 @@ func (g *Generator) Run(ctx context.Context, min, max time.Duration) {
r := g.Make(time.Now())
if err := g.store.Append(r); err != nil {
log.Printf("generator: storing release: %v", err)
logger.Error("storing release failed", "error", err)
continue
}
log.Printf("generator: published %s (%s) %s", r.ID, r.Tag[0], r.OCID)
logger.Info("published release", "release_id", r.ID, "tag", r.Tag[0], "ocid", r.OCID)
}
}