Logging In Loop (ARCH5164)
Ce contenu n’est pas encore disponible dans votre langue.
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.
Incorrect
Section titled “Incorrect”func process(log *slog.Logger, items []Item) { for _, it := range items { log.Info("processing item", "id", it.ID) handle(it) }}Correct
Section titled “Correct”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.