feat: adds configurable HTTP request path exclusion from logging
This commit is contained in:
@@ -10,6 +10,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/config"
|
||||
)
|
||||
|
||||
var ExcludeFromLogging = regexp.MustCompile(`\/(ready|live|metrics)$`)
|
||||
@@ -21,12 +23,22 @@ type LoggingResponseWriter struct {
|
||||
}
|
||||
|
||||
func loggingMiddleware(appCtx context.Context, next http.Handler) http.Handler {
|
||||
appConfig := config.MustFromCtx(appCtx)
|
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if ExcludeFromLogging.Match([]byte(r.URL.Path)) {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
// User-configurable logging exclusions
|
||||
for _, re := range appConfig.HTTP.GetExcludeRegexps() {
|
||||
if re.MatchString(r.URL.Path) {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
log := zerolog.Ctx(appCtx)
|
||||
|
||||
start := time.Now()
|
||||
|
Reference in New Issue
Block a user