Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/go-chi/httplog
Go HTTP request logger with structured logging capabilities built on "log/slog" package
https://github.com/go-chi/httplog
Last synced: 3 days ago
JSON representation
Go HTTP request logger with structured logging capabilities built on "log/slog" package
- Host: GitHub
- URL: https://github.com/go-chi/httplog
- Owner: go-chi
- License: mit
- Created: 2020-04-01T19:45:25.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-07-16T14:40:55.000Z (4 months ago)
- Last Synced: 2024-11-06T05:17:37.798Z (10 days ago)
- Language: Go
- Homepage:
- Size: 50.8 KB
- Stars: 206
- Watchers: 5
- Forks: 42
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
httplog
=======Small but powerful structured logging package for HTTP request logging built
on the Go 1.21+ stdlib `slog` package.```
go get -u github.com/go-chi/httplog/v2
```## Example
(see [_example/](./_example/main.go))
```go
package mainimport (
"log/slog"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/httplog/v2"
)func main() {
// Logger
logger := httplog.NewLogger("httplog-example", httplog.Options{
// JSON: true,
LogLevel: slog.LevelDebug,
Concise: true,
RequestHeaders: true,
MessageFieldName: "message",
// TimeFieldFormat: time.RFC850,
Tags: map[string]string{
"version": "v1.0-81aa4244d9fc8076a",
"env": "dev",
},
QuietDownRoutes: []string{
"/",
"/ping",
},
QuietDownPeriod: 10 * time.Second,
// SourceFieldName: "source",
})// Service
r := chi.NewRouter()
r.Use(httplog.RequestLogger(logger))
r.Use(middleware.Heartbeat("/ping"))r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
httplog.LogEntrySetField(ctx, "user", slog.StringValue("user1"))
next.ServeHTTP(w, r.WithContext(ctx))
})
})r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world"))
})r.Get("/panic", func(w http.ResponseWriter, r *http.Request) {
panic("oh no")
})r.Get("/info", func(w http.ResponseWriter, r *http.Request) {
oplog := httplog.LogEntry(r.Context())
w.Header().Add("Content-Type", "text/plain")
oplog.Info("info here")
w.Write([]byte("info here"))
})r.Get("/warn", func(w http.ResponseWriter, r *http.Request) {
oplog := httplog.LogEntry(r.Context())
oplog.Warn("warn here")
w.WriteHeader(400)
w.Write([]byte("warn here"))
})r.Get("/err", func(w http.ResponseWriter, r *http.Request) {
oplog := httplog.LogEntry(r.Context())
oplog.Error("msg here", "err", errors.New("err here"))
w.WriteHeader(500)
w.Write([]byte("oops, err"))
})http.ListenAndServe("localhost:8000", r)
}```
## License
MIT