Request-scoped slog logger with JSON output (Go 1.21+)

2564
0

I’ve started using log/slog for services that want structured logs without a heavy dependency. The key is treating the request logger as data: create a base logger with JSON output, then derive a request logger with fields like request_id and path. I avoid logging raw request bodies by default and instead log stable identifiers and error codes. Because slog uses context.Context, it’s easy to pass the logger down without changing every function signature. In production, this makes logs far more useful for queries like “show all errors for request_id=...” and it keeps log formatting consistent across packages. It’s a small wrapper, but it makes the whole codebase feel more coherent.