logginghandler/logginghandler.go
Marvin Preuss 930e291456
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
feat!: splitting Logger function in FromRequest and FromCtx
2022-01-24 11:45:27 +01:00

59 lines
1.5 KiB
Go

// Package logginghandler is a simple, zerolog based, request logging http middleware.
// It also sets `X-Request-ID` in the request and response headers.
package logginghandler
import (
"context"
"net/http"
"time"
"github.com/justinas/alice"
"github.com/rs/zerolog"
"github.com/rs/zerolog/hlog"
"github.com/rs/zerolog/log"
)
// GetUUID gets the requests UUID from a request.
func GetUUID(r *http.Request) (string, bool) {
uuid, ok := hlog.IDFromRequest(r)
if !ok {
return "", false
}
return uuid.String(), true
}
// FromRequest returns a logger with the UUID set from request.
func FromRequest(r *http.Request) zerolog.Logger {
return *hlog.FromRequest(r)
}
// FromCtx returns a logger with the UUID set from ctx.
func FromCtx(ctx context.Context) zerolog.Logger {
return *log.Ctx(ctx)
}
func Handler(log zerolog.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
chain := alice.New(
hlog.NewHandler(log),
hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
hlog.FromRequest(r).Info().
Str("method", r.Method).
Str("proto", r.Proto).
Stringer("request-url", r.URL).
Int("status", status).
Int("size", size).
Dur("duration", duration).
Msg("")
}),
hlog.RemoteAddrHandler("remote"),
hlog.UserAgentHandler("user-agent"),
hlog.RefererHandler("referer"),
hlog.RequestIDHandler("uuid", "X-Request-ID"),
).Then(next)
return chain
}
}