home / skills / hoangnguyen0403 / agent-skills-standard / logging
This skill enforces structured, contextual Golang logging with leveled outputs and safe library usage to improve observability.
npx playbooks add skill hoangnguyen0403/agent-skills-standard --skill loggingReview the files below or copy the command above to add this skill to your agents.
---
name: Logging
description: Standards for structured logging and observability in Golang.
metadata:
labels: [golang, logging, slog, zap, observability]
triggers:
files: ['go.mod', 'pkg/logger/**']
keywords: [logging, slog, structured logging, zap]
---
# Golang Logging Standards
## **Priority: P1 (STANDARD)**
## Principles
- **Structured Logging**: Use JSON or structured text. Readable by machines and humans.
- **Leveled Logging**: Debug, Info, Warn, Error.
- **Contextual**: Include correlation IDs (TraceID, RequestID) in logs.
- **No `log.Fatal`**: Avoid terminating app inside libraries. Return error instead. Only `main` should exit.
## Libraries
- **`log/slog` (Recommended)**: Stdlib since Go 1.21. Fast, structured, zero-dep.
- **Zap (`uber-go/zap`)**: High performance, good if pre-1.21 or extreme throughput needed.
- **Zerolog**: Zero allocation, fast JSON logger.
## Guidelines
- Initialize logger at startup.
- Inject logger or use a global singleton configured at startup (pragmatic choice).
- Use `slog.Attr` for structured data.
## References
- [Slog Patterns](references/slog-patterns.md)
This skill documents standards for structured logging and observability in Go applications. It focuses on consistent, machine-readable logs, leveled logging, and contextual fields like trace and request IDs to improve debuggability and tracing across services. The guidance favors Go 1.21's slog but covers high-performance alternatives for special cases.
The skill describes what to capture in logs, which libraries to prefer, and how to initialize and propagate loggers across an application. It prescribes structured JSON logs, common levels (Debug, Info, Warn, Error), and inclusion of contextual fields such as TraceID or RequestID on every relevant entry. It also explains lifecycle rules: configure logger at startup, inject or provide a singleton, and avoid terminating the app from libraries.
Should I always use slog in Go?
Prefer slog for most projects because it is in the standard library (Go 1.21+) and provides structured, zero-dependency logging. Use zap or zerolog only if you need extreme performance or are on an older Go version.
How do I attach TraceID to logs?
Extract TraceID from request context (or tracing library) in middleware, then include it as a structured attribute on every log entry so logs can be correlated with traces and metrics.