Skip to content

Logging In Loop (ARCH5164)

Key go.logging.logging-in-loop · Severity info · Category logging

Logging on every iteration of a tight loop floods log sinks and buries real signal. The loop outcome should usually be logged with a single summary line.

func process(log *slog.Logger, items []Item) {
for _, it := range items {
log.Info("processing item", "id", it.ID)
handle(it)
}
}
func process(log *slog.Logger, items []Item) {
for _, it := range items {
handle(it)
}
log.Info("processed items", "count", len(items))
}

Move the log call out of the loop and collapse it into a single summary line.