Go handles logging with the standard library: the older log package for simple text lines and log/slog (added in Go 1.21) for structured, leveled logging with pluggable handlers. For production, write JSON logs with slog to standard output, let your platform collect them, and analyse them in a log system such as Loki, Elasticsearch or a cloud logging service.
Go itself does not store or search logs. Management means producing consistent structured records, controlling volume, and shipping them elsewhere.
The log package writes a timestamped line to standard error by default.
It has no levels, no structured fields and no JSON output. log.Fatal exits without running deferred functions, and log.Panic panics. Keep log for small tools; use slog for services.
log/slog produces records with a time, level, message and key-value attributes. A handler decides the output format: slog.NewTextHandler for humans and slog.NewJSONHandler for machines.
Output (the time value differs on each run):
Attach fields once with logger.With, group related attributes with slog.Group, and use the Context variants so handlers can read request-scoped values.
For typed, allocation-friendly calls, use LogAttrs with slog.String, slog.Int and similar helpers.
Use slog.LevelVar to switch to debug logging without a restart, for example from an admin endpoint or a signal handler.
Never log passwords, tokens or full card numbers. HandlerOptions.ReplaceAttr lets you rewrite or drop attributes in one place.
A type can also implement slog.LogValuer to control how it appears in logs. If you need extra features such as sampling or very high throughput, go.uber.org/zap and github.com/rs/zerolog remain popular, and both can be used behind an slog.Handler.
Follow the twelve-factor approach: write logs to standard output and let the environment collect them. Do not manage log files or rotation inside a containerised app.
level, request_id and status.When you do write files on a server, use rotation from the operating system (logrotate) or a library such as gopkg.in/natefinch/lumberjack.v2.
For quick local analysis, JSON logs work well with jq:
Add a correlation field such as request_id or an OpenTelemetry trace ID to every record so one request can be followed across services.
slog with a JSON handler in production and a text handler in development.fmt.Errorf("...: %w", err) as they travel up.logger.Enabled(ctx, slog.LevelDebug) before building expensive debug data.Use slog for anything beyond a small script. It is in the standard library, supports levels and structured fields, and works with any backend through handlers.
No. Write to stdout and let your platform manage storage, or use logrotate or a rotation library when logging to files.
Often not. slog covers most needs. They remain useful when you need their specific features or are maintaining existing code.
Create a child logger with logger.With("request_id", id) in middleware and pass it down through the request context or your handler struct.
Go gives you log for simple output and slog for structured, leveled logging. Emit JSON to stdout, add correlation fields, redact secrets and control volume. Collection, storage and analysis belong to external tools such as Loki or OpenSearch, with jq for quick local checks.