From 010e54168a83f1fea808d17ef81bda1a09cdd2ae Mon Sep 17 00:00:00 2001 From: surtur Date: Mon, 4 Sep 2023 14:01:42 +0200 Subject: [PATCH] go(app/server): skip logging /assets visits --- app/server.go | 19 ++++++++++++++++++- app/settings/settings.go | 6 ++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/app/server.go b/app/server.go index 7f69810..b098dda 100644 --- a/app/server.go +++ b/app/server.go @@ -6,11 +6,13 @@ package app import ( "encoding/hex" "net/http" + "regexp" "github.com/gorilla/sessions" "github.com/labstack/echo-contrib/session" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" + "golang.org/x/exp/slog" "golang.org/x/time/rate" ) @@ -20,7 +22,22 @@ func (a *App) SetServerSettings() { e.HideBanner = true - e.Use(middleware.Logger()) + if a.setting.DefaultLoggerSkipAssets() { + re := regexp.MustCompile("^/(assets|static)(.*)|favicon.ico") + lC := middleware.DefaultLoggerConfig + + lC.Skipper = func(c echo.Context) bool { + r := c.Request().URL.Path + + slog.Debug("logger skipper", "path", r) + + return re.MatchString(r) + } + e.Use(middleware.LoggerWithConfig(lC)) + } else { + e.Use(middleware.Logger()) + } + // e.Use(middleware.LoggerWithConfig( // middleware.LoggerConfig{ // Format: `{"time":"${time_rfc3339_nano}","id":"${id}","remote_ip":"${remote_ip}",` + diff --git a/app/settings/settings.go b/app/settings/settings.go index df7f4d1..c3f2a30 100644 --- a/app/settings/settings.go +++ b/app/settings/settings.go @@ -49,6 +49,7 @@ const ( defaultHTTPDomain = "localhost" defaultServerWriteTimeout = 30 * time.Second defaultServerReadHeaderTimeout = 30 * time.Second + defaultLoggerSkipAssets = true ) // cleantgt is a list of ENV vars pertaining to pcmt. @@ -78,6 +79,11 @@ func (s *Settings) DefaultServerReadHeaderTimeout() time.Duration { return defaultServerReadHeaderTimeout } +// DefaultLoggerSkipAssets returns whether the logger skips reporting asset visits. +func (s *Settings) DefaultLoggerSkipAssets() bool { + return defaultLoggerSkipAssets +} + // Consolidate reconciles whatever values are set in config and via flags and // sets it to one place that should be regarded as a single source of truth - // the settings struct. Order of preference for values is (from higher to