Instruction file imported from ncosentino/needlr (
.github/instructions/genesis/go/logging.instructions.md). Copyright stays with the author.
Go logging
Use the standard library log/slog for structured logging. It needs no external
dependency and is the current ecosystem default for command-line tools.
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.LevelInfo,
}))
slog.SetDefault(logger)
slog.Info("starting", "addr", addr, "version", version)
Rules:
- Log to stderr, not stdout — stdout is reserved for the command's actual output so it can be piped and parsed.
- Use structured key/value attributes (
slog.Info("msg", "key", value)); do not format variable data into the message string. - Use lowercase, constant message strings and
camelCaseattribute keys. - Do not log and return the same error — wrap and return it, and let the single top-level handler decide whether to print it.
- Use
fmt.Fprintln(cmd.OutOrStdout(), ...)for user-facing command output; reserveslogfor diagnostics.