go(app/server): skip logging /assets visits
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2023-09-04 14:01:42 +02:00
parent 15994c9d8f
commit 010e54168a
Signed by: wanderer
SSH Key Fingerprint: SHA256:MdCZyJ2sHLltrLBp0xQO0O1qTW9BT/xl5nXkDvhlMCI
2 changed files with 24 additions and 1 deletions

View File

@ -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}",` +

View File

@ -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