2020-10-13 15:26:40 +02:00
|
|
|
# logginghandler
|
|
|
|
|
|
|
|
[![Build Status](https://ci.xsfx.dev/api/badges/xsteadfastx/logginghandler/status.svg)](https://ci.xsfx.dev/xsteadfastx/logginghandler)
|
2021-01-18 11:59:19 +01:00
|
|
|
[![Go Reference](https://pkg.go.dev/badge/go.xsfx.dev/logginghandler.svg)](https://pkg.go.dev/go.xsfx.dev/logginghandler)
|
|
|
|
[![Go Report Card](https://goreportcard.com/badge/go.xsfx.dev/logginghandler)](https://goreportcard.com/report/go.xsfx.dev/logginghandler)
|
2020-10-13 15:26:40 +02:00
|
|
|
|
|
|
|
Just a simple zerolog based request logging http middleware. It also sets a `X-Request-ID` in the request and response headers.
|
|
|
|
|
|
|
|
## Install
|
|
|
|
|
2021-01-18 11:02:17 +01:00
|
|
|
go get -v go.xsfx.dev/logginghandler
|
2020-10-13 15:26:40 +02:00
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
handler := logginghandler.Handler(http.HandlerFunc(myHandler))
|
2020-12-03 11:10:17 +01:00
|
|
|
http.Handle("/", handler)
|
2020-10-13 15:26:40 +02:00
|
|
|
log.Fatal().Msg(http.ListenAndServe(":5000", nil).Error())
|
|
|
|
|
|
|
|
In other handlers you can access the UUID:
|
|
|
|
|
|
|
|
func anotherHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintf(w, "your uuid is: %s", logginghandler.GetUUID(r))
|
|
|
|
}
|
|
|
|
|
|
|
|
The already prepared logger is also available:
|
|
|
|
|
2021-02-16 13:37:43 +01:00
|
|
|
l := logginghandler.Logger(r)
|
2020-10-13 15:26:40 +02:00
|
|
|
l.Info().Msg("foo bar")
|