Building Slogbox
5 days ago
- #Go programming
- #slog
- #logging
- slogbox is a structured log handler that stores recent logs in a fixed-size ring buffer, inspired by Go's runtime/trace.FlightRecorder.
- It stores raw slog.Record values for flexibility, only serializing on read, and resolves dynamic attributes at log time for accuracy.
- The design prioritizes write performance using a mutex for concurrency, with efficient snapshotting, age filtering via binary search, and shared buffers for derived handlers.
- It supports level-triggered flushes, explicit flushing for graceful shutdown, and HTTP endpoints for debugging, while being memory-efficient and suitable for health checks or black-box recording.
- Use cases include health check endpoints, debugging, lightweight services, and multi-handler setups, but it's not for persistent logs or cross-instance aggregation.