https://github.com/mucansever/widelogger
package for wide logs containing all the context
https://github.com/mucansever/widelogger
go golang logging
Last synced: 5 months ago
JSON representation
package for wide logs containing all the context
- Host: GitHub
- URL: https://github.com/mucansever/widelogger
- Owner: mucansever
- License: mit
- Created: 2025-12-29T23:27:08.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2026-01-17T16:27:04.000Z (5 months ago)
- Last Synced: 2026-01-18T00:06:19.486Z (5 months ago)
- Topics: go, golang, logging
- Language: Go
- Homepage:
- Size: 26.4 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# widelogger
Context-based structured logging for Go with field accumulation. Collects log fields throughout a request lifecycle and emits them all at once.
Inspired by: https://loggingsucks.com
## Installation
```bash
go get github.com/mucansever/widelogger
```
## Basic Usage
Accumulate fields in `context.Context` and log them together.
```go
ctx := widelogger.NewContext(context.Background())
widelogger.AddFields(ctx, "user_id", 123)
widelogger.AddWarning(ctx, "slow query", "ms", 500)
widelogger.Info(ctx, "request completed")
```
## HTTP Middleware
You can transform your HTTP request lifecycles into widelogs easily with the middleware.
```go
mux := http.NewServeMux()
handler := widelogger.Middleware(mux,
widelogger.WithIncludeRequestHeaders("User-Agent"),
widelogger.WithExcludePaths("/health"),
// log only 10% of successful requests to save space.
// requests with warnings, errors, or status >= 400 are always logged.
widelogger.WithSuccessSampling(0.1),
)
http.ListenAndServe(":8080", handler)
```
## Why widelogger?
Instead of multiple scattered log lines, you get one "wide" log entry containing everything that happened during that request.
```json
{
"time": "2026-01-17T12:00:00Z",
"level": "INFO",
"msg": "http_request_completed",
"method": "GET",
"path": "/api/user",
"user_id": 123,
"duration_ms": 45,
"status_code": 200,
"warnings": [{"message": "slow query", "fields": {"ms": 500}}]
}
```
You can play with the sample server provided in `examples/basic/server.go` by running it with `go run examples/basic/server.go`.