Skip to content

Add structured JSON logging#31

Merged
botre merged 1 commit into
masterfrom
feat/structured-logging
May 15, 2026
Merged

Add structured JSON logging#31
botre merged 1 commit into
masterfrom
feat/structured-logging

Conversation

@botre
Copy link
Copy Markdown
Collaborator

@botre botre commented May 15, 2026

What

Replaces the unstructured stdlib log calls with log/slog emitting one JSON object per line to stdout. The app stays backend-agnostic — the decoupling boundary is stdout, so any collector can pick the logs up.

Changes

  • src/logging/slog JSON setup: OpenTelemetry semantic-convention field names (service.name, http.request.method, url.path, ...), a context-aware handler that stamps request_id onto every record, a sensitive-key redaction backstop, and LOG_LEVEL gating (default info in production, debug elsewhere).
  • src/requestlog.go — Fiber access-log middleware. Each request reuses a valid inbound X-Request-Id (^[A-Za-z0-9-]{8,64}$) or mints a UUID, echoes it on the response header, and emits one structured line. Headers and bodies are never logged; the path is logged without its query string. Kubernetes probe traffic to /api/health logs at debug.
  • src/database/context.Context threaded through the query functions so a query error correlates to its originating request.
  • README.md — logging section.

Verification

go vet / go build / go test ./... all pass. Runtime smoke test confirmed JSON output, OTel field names, request_id correlation (inbound reuse, regex rejection, response-header echo), and probe hits producing zero lines at info level. No new dependencies — slog is stdlib and uuid was already in use.

Replace the unstructured stdlib `log` calls with `log/slog` emitting one
JSON object per line to stdout, so the logs are ready for a collector
without coupling the app to any backend.

- src/logging: slog JSON setup with OpenTelemetry field names, a
  context-aware handler that stamps request_id, sensitive-key redaction,
  and LOG_LEVEL gating (info in prod, debug elsewhere).
- src/requestlog.go: Fiber access-log middleware — validates or mints an
  X-Request-Id, echoes it on the response, logs one line per request
  (path query-stripped, no headers/bodies), probes at debug.
- Thread context.Context through the database package so query errors
  correlate to their request.
@botre botre merged commit 871d76f into master May 15, 2026
2 checks passed
@botre botre deleted the feat/structured-logging branch May 15, 2026 12:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant