docs(README): alice example
This commit is contained in:
parent
b57a91bda5
commit
fd35c0dccc
61
README.md
61
README.md
@ -6,24 +6,65 @@
|
|||||||
|
|
||||||
Just a simple zerolog based request logging http middleware. It also sets a `X-Request-ID` in the request and response headers.
|
Just a simple zerolog based request logging http middleware. It also sets a `X-Request-ID` in the request and response headers.
|
||||||
|
|
||||||
|
Powered by [github.com/rs/zerolog/hlog](https://github.com/rs/zerolog) and [github.com/justinas/alice](https://github.com/justinas/alice).
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
go get -v go.xsfx.dev/logginghandler
|
```shell
|
||||||
|
go get -v go.xsfx.dev/logginghandler
|
||||||
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
logger := log.With().Logger()
|
```golang
|
||||||
handler := logginghandler.Handler(logger)(http.HandlerFunc(myHandler))
|
logger := log.With().Logger()
|
||||||
http.Handle("/", handler)
|
|
||||||
log.Fatal().Msg(http.ListenAndServe(":5000", nil).Error())
|
handler := logginghandler.Handler(logger)(
|
||||||
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
|
||||||
|
log := logginghander.Logger(r)
|
||||||
|
log.Info().Msg("hello world")
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
|
return
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
http.Handle("/", handler)
|
||||||
|
log.Fatal().Msg(http.ListenAndServe(":5000", nil).Error())
|
||||||
|
```
|
||||||
|
|
||||||
|
or with [alice](https://github.com/justinas/alice)
|
||||||
|
|
||||||
|
```golang
|
||||||
|
logger := log.With().Logger()
|
||||||
|
chain := alice.New(logginghandler.Handler(logger)).Then(
|
||||||
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
|
||||||
|
log := logginghander.Logger(r)
|
||||||
|
log.Info().Msg("hello world")
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
|
return
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
http.Handle("/", chain)
|
||||||
|
|
||||||
|
log.Fatal().Err(http.ListenAndServe(":5000", nil)).Msg("goodbye")
|
||||||
|
```
|
||||||
|
|
||||||
In other handlers you can access the UUID:
|
In other handlers you can access the UUID:
|
||||||
|
|
||||||
func anotherHandler(w http.ResponseWriter, r *http.Request) {
|
```golang
|
||||||
fmt.Fprintf(w, "your uuid is: %s", logginghandler.GetUUID(r))
|
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:
|
The already prepared logger is also available:
|
||||||
|
|
||||||
l := logginghandler.Logger(r)
|
```golang
|
||||||
l.Info().Msg("foo bar")
|
l := logginghandler.Logger(r)
|
||||||
|
l.Info().Msg("foo bar")
|
||||||
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user