Skip to content
Back to skills

Golang Logging

ASecurity

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: ```...

  • 584 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added September 5, 2026
developmentgo

Security analysis

A100/100

Pro scans all 11 files and shows the line behind each finding

Scanned September 5, 2026

npx -y skills add HoangNguyen0403/agent-skills-standard --skill golang-logging --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Golang Logging?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Golang Logging
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/hoangnguyen0403-golang-logging-agent-skills-standard/badge)](https://www.skillsdirectory.com/skills/hoangnguyen0403-golang-logging-agent-skills-standard)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
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.md1.4 KB
  • eval-1.with-skill.md1.4 KB
  • eval-2.baseline.md1.1 KB
  • eval-2.with-skill.md2.3 KB
  • eval-3.baseline.md744 B
  • eval-3.with-skill.md643 B
  • trigger-1.md152 B
  • trigger-2.md165 B
  • trigger-3.md133 B
  • trigger-4.md139 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.