Add structured JSON logging#31
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Replaces the unstructured stdlib
logcalls withlog/slogemitting 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/—slogJSON setup: OpenTelemetry semantic-convention field names (service.name,http.request.method,url.path, ...), a context-aware handler that stampsrequest_idonto every record, a sensitive-key redaction backstop, andLOG_LEVELgating (defaultinfoin production,debugelsewhere).src/requestlog.go— Fiber access-log middleware. Each request reuses a valid inboundX-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/healthlogs atdebug.src/database/—context.Contextthreaded 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_idcorrelation (inbound reuse, regex rejection, response-header echo), and probe hits producing zero lines atinfolevel. No new dependencies —slogis stdlib anduuidwas already in use.