Golang Logging
ASecurityUse Go's `log/slog` for structured logging. Set it up once in `main()` with a JSON handler, then inject a logger into your HTTP server and middleware. For each request, attach contextual fields like `request_id`, `trace_id`, `method`, `path`, and `status` so logs are machine-readable and easy to filter. Prefer leveled logs: - `Debug` for detailed diagnostics - `Info` for normal request lifecycle events - `Warn` for unusual but recoverable conditions - `Error` for failures Example pattern: ```...
- 584 stars
- 0 votes
- 0 copies
- 4 views
- Added September 5, 2026
Security analysis
100/100Pro scans all 11 files and shows the line behind each finding
npx -y skills add HoangNguyen0403/agent-skills-standard --skill golang-logging --agent claude-codeAre you the author of Golang Logging?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/hoangnguyen0403-golang-logging-agent-skills-standard)Use Go's `log/slog` for structured logging.
Set it up once in `main()` with a JSON handler, then inject a logger into your HTTP server and middleware. For each request, attach contextual fields like `request_id`, `trace_id`, `method`, `path`, and `status` so logs are machine-readable and easy to filter.
Prefer leveled logs:
- `Debug` for detailed diagnostics
- `Info` for normal request lifecycle events
- `Warn` for unusual but recoverable conditions
- `Error` for failures
Example pattern:
```go
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
reqID := r.Header.Get("X-Request-ID")
traceID := r.Header.Get("X-Trace-ID")
reqLogger := logger.With(
slog.String("request_id", reqID),
slog.String("trace_id", traceID),
slog.String("method", r.Method),
slog.String("path", r.URL.Path),
)
ctx := context.WithValue(r.Context(), loggerKey{}, reqLogger)
reqLogger.Info("request started")
next.ServeHTTP(w, r.WithContext(ctx))
reqLogger.Info("request completed")
})
}
```
Avoid `fmt.Println` and unstructured strings in production. Also avoid `log.Fatal` inside libraries or handlers; return errors upward and let only `main()` decide whether to exit.
Files in this skill
- eval-1.baseline.md
- eval-1.with-skill.md
- eval-2.baseline.md
- eval-2.with-skill.md
- eval-3.baseline.md
- eval-3.with-skill.md
- trigger-1.md
- trigger-2.md
- trigger-3.md
- trigger-4.md
Attribution
Related skills
View all in development →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.