Extend logging middleware with more config options
This commit is contained in:
parent
4b8a62ba12
commit
506834c881
1 changed files with 21 additions and 4 deletions
|
@ -5,19 +5,36 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/hlog"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func BuildLoggingMiddleware(extras map[string]string) HandlerBuilder {
|
||||
func BuildLoggingMiddleware(
|
||||
status500IsError bool,
|
||||
ignorePaths []string,
|
||||
extras map[string]string,
|
||||
) HandlerBuilder {
|
||||
return func(h http.Handler) http.Handler {
|
||||
return ChainMiddlewares(h,
|
||||
hlog.NewHandler(log.Logger),
|
||||
hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
|
||||
if strings.HasPrefix(r.URL.Path, "/assets") {
|
||||
return
|
||||
for _, p := range ignorePaths {
|
||||
if strings.HasPrefix(r.URL.Path, p) {
|
||||
return
|
||||
}
|
||||
}
|
||||
logger := hlog.FromRequest(r).Info().
|
||||
var logger *zerolog.Event
|
||||
if status >= 500 {
|
||||
if status500IsError {
|
||||
logger = hlog.FromRequest(r).Error()
|
||||
} else {
|
||||
logger = hlog.FromRequest(r).Warn()
|
||||
}
|
||||
} else {
|
||||
logger = hlog.FromRequest(r).Info()
|
||||
}
|
||||
logger = logger.
|
||||
Str("method", r.Method).
|
||||
Stringer("url", r.URL).
|
||||
Int("status", status).
|
||||
|
|
Loading…
Reference in a new issue