Go to file
Marvin Preuss 642cbbabd5
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
chore: upgrade tools
2022-01-17 11:41:25 +01:00
third_party/tools build: uses go modules for tool handling 2022-01-14 13:51:56 +01:00
vendor chore: upgrade tools 2022-01-17 11:41:25 +01:00
.drone.yml build: uses go modules for tool handling 2022-01-14 13:51:56 +01:00
.gitignore envrc gitignore 2021-02-16 14:27:26 +01:00
.goreleaser.yml first commit 2020-10-13 15:26:40 +02:00
go.mod chore: upgrade tools 2022-01-17 11:41:25 +01:00
go.sum chore: upgrade tools 2022-01-17 11:41:25 +01:00
LICENSE adds MIT license 2021-01-18 11:20:31 +01:00
logginghandler_test.go test: write at least status code in example 2022-01-17 11:28:17 +01:00
logginghandler.go feat: uses alice to better readability of the chained handlers and adds access logging again 2022-01-17 11:27:49 +01:00
Makefile build: disabling test cache and ignoring varnamelen in lint 2022-01-17 11:26:34 +01:00
README.md docs(README): alice example 2022-01-17 11:28:33 +01:00

logginghandler

Build Status Go Reference Go Report Card

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 and github.com/justinas/alice.

Install

go get -v go.xsfx.dev/logginghandler

Usage

logger := log.With().Logger()

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

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:

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:

l := logginghandler.Logger(r)
l.Info().Msg("foo bar")