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

+11 -8
View File
@@ -62,7 +62,7 @@ func confFor(apiRoot string) *config.JobSearchConfig {
}
func TestFetchFunctions(t *testing.T) {
fetchers := map[string]func(context.Context, *config.JobSearchConfig, time.Time) error{
fetchers := map[string]runFunc{
"FindAOpportunity": fetchFindAOpportunity,
"FTSMock": fetchFTSMock,
}
@@ -91,7 +91,9 @@ func TestFetchFunctions(t *testing.T) {
for _, tc := range tests {
t.Run(fname+"/"+tc.name, func(t *testing.T) {
srv, hits := ftsServer(t, tc.status, tc.body)
err := fetch(context.Background(), confFor(srv.URL), time.Now().Add(-time.Hour))
err := fetch(
context.Background(), slog.Default(), confFor(srv.URL), time.Now().Add(-time.Hour),
)
if !tc.check(err) {
t.Fatalf("err = %v, want %s", err, tc.wantErr)
}
@@ -110,11 +112,11 @@ func TestFetchUsesConfiguredAPIRoot(t *testing.T) {
conf.Sources.FindATender.APIRoot = fat.URL
conf.Sources.FTSMock.APIRoot = mock.URL
fetchFindAOpportunity(context.Background(), conf, time.Time{})
fetchFindAOpportunity(context.Background(), slog.Default(), conf, time.Time{})
if fatHits.Load() != 1 || mockHits.Load() != 0 {
t.Fatalf("FindAOpportunity hits: fat=%d mock=%d", fatHits.Load(), mockHits.Load())
}
fetchFTSMock(context.Background(), conf, time.Time{})
fetchFTSMock(context.Background(), slog.Default(), conf, time.Time{})
if fatHits.Load() != 1 || mockHits.Load() != 1 {
t.Fatalf("FTSMock hits: fat=%d mock=%d", fatHits.Load(), mockHits.Load())
}
@@ -126,7 +128,8 @@ func TestFetchCancelled(t *testing.T) {
// Unroutable root: the request should never be waited on.
conf := confFor("http://192.0.2.1:9")
start := time.Now()
if err := fetchFTSMock(ctx, conf, time.Time{}); !errors.Is(err, context.Canceled) {
err := fetchFTSMock(ctx, slog.Default(), conf, time.Time{})
if !errors.Is(err, context.Canceled) {
t.Fatalf("err = %v, want context.Canceled", err)
}
if d := time.Since(start); d > time.Second {
@@ -163,7 +166,7 @@ func TestRunSleeperShutdown(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() {
runSleeper(ctx, conf, time.Time{})
runSleeper(ctx, slog.Default(), conf, time.Time{})
close(done)
}()
time.Sleep(10 * time.Millisecond) // let the first sleep start
@@ -184,13 +187,13 @@ func TestRunSleeperShutdown(t *testing.T) {
func TestRunWorkersRejectZeroInterval(t *testing.T) {
conf := &config.JobSearchConfig{}
runs := map[string]func(context.Context, *config.JobSearchConfig, time.Time) error{
runs := map[string]runFunc{
"FindAOpportunity": runFindAOpportunity,
"FTSMock": runFTSMock,
"Sleeper": runSleeper,
}
for name, run := range runs {
if err := run(context.Background(), conf, time.Time{}); err == nil {
if err := run(context.Background(), slog.Default(), conf, time.Time{}); err == nil {
t.Errorf("%s: expected an error for a zero interval", name)
}
}