Reviewer · Logging
Structured log fields
When attaching multiple pieces of contextual data to a log entry, use a single `WithFields(log.Fields{...})` call instead of chaining multiple `WithField()`/`WithError()` calls. Each `WithXxx` call allocates a new logger entry, so chaining them wastes allocations and obscures intent; a single `WithFields` call is both more efficient and easier to read.
When attaching multiple pieces of contextual data to a log entry, use a single WithFields(log.Fields{...}) call instead of chaining multiple WithField()/WithError() calls. Each WithXxx call allocates a new logger entry, so chaining them wastes allocations and obscures intent; a single WithFields call is both more efficient and easier to read.
// Avoid: each WithXxx call allocates a new entry
log.G(ctx).WithError(err).WithField("target", target).Error("failed to deactivate mounts")
// Prefer: all context in one call
log.G(ctx).WithFields(log.Fields{
"error": err,
"target": target,
}).Error("failed to deactivate mounts")
This applies both to one-off log statements and to loops where a base logger is derived once and then extended per-iteration (e.g. logger := log.G(ctx).WithFields(...) followed by logger.WithField(...) inside a hot loop) — prefer building the full field set once, or merging fields in a single call, to avoid repeated allocations and logger shadowing. Additionally, ensure the log message itself carries enough standalone context (relevant IDs, observed state, or error) since log output typically lacks file/line information and may be read without surrounding code.