Gate unknown location logging on requestLogEnable

This commit is contained in:
gr-dv committed 2026-11-25 16:06:11 +03:00
1 parent b6cf6ed2bb
commit fe8b5511f1
3 files changed
+36 -6

No files matched your search

+1 -1
View File
@@ -189,7 +189,7 @@ func (l Locator) Handler(config conf.Remote, locations []conf.Location) (http.Ha
mux.PathPrefix(location.PathPrefix).Handler(entrypoint)
}
mux.NotFoundHandler = middleware.UnknownLocation(l.logger, skipLogging)
mux.NotFoundHandler = middleware.UnknownLocation(l.logger, config.Logging.RequestLogEnable, skipLogging)
return mux, nil
}
+3 -2
View File
@@ -7,9 +7,10 @@ import (
)
// UnknownLocation — catch-all для запросов, не попавших ни в один location.
func UnknownLocation(logger log.Logger, skipLogging SkipLoggingPaths) http.Handler {
// Warn пишется при включённом логировании запросов (как у log request).
func UnknownLocation(logger log.Logger, enableRequestLogging bool, skipLogging SkipLoggingPaths) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !skipLogging.ShouldSkip(r.URL.Path) {
if enableRequestLogging && !skipLogging.ShouldSkip(r.URL.Path) {
logger.Warn(r.Context(), "request to unknown location",
log.String("httpMethod", r.Method),
log.String("path", r.URL.Path),
+32 -3
View File
@@ -29,7 +29,10 @@ func (s *UnknownLocationsLoggingTestSuite) Test_UnknownPath_LogsWarn() {
capture := &logCapture{}
locator := assembly.NewLocator(capture, nil, nil, routes.NewRoutes(test.Logger()), nil, nil, nil, nil, nil)
handler, err := locator.Handler(conf.Remote{Http: conf.Http{MaxRequestBodySizeInMb: 1}}, nil)
handler, err := locator.Handler(conf.Remote{
Http: conf.Http{MaxRequestBodySizeInMb: 1},
Logging: conf.Logging{RequestLogEnable: true},
}, nil)
require.NoError(err)
srv := httptest.NewServer(handler)
@@ -58,8 +61,11 @@ func (s *UnknownLocationsLoggingTestSuite) Test_UnknownPath_SuppressionSkipsWarn
capture := &logCapture{}
config := conf.Remote{
Http: conf.Http{MaxRequestBodySizeInMb: 1},
Logging: conf.Logging{SkipLoggingPathPrefixes: []string{"/unknown"}},
Http: conf.Http{MaxRequestBodySizeInMb: 1},
Logging: conf.Logging{
RequestLogEnable: true,
SkipLoggingPathPrefixes: []string{"/unknown"},
},
}
locator := assembly.NewLocator(capture, nil, nil, routes.NewRoutes(test.Logger()), nil, nil, nil, nil, nil)
handler, err := locator.Handler(config, nil)
@@ -76,6 +82,29 @@ func (s *UnknownLocationsLoggingTestSuite) Test_UnknownPath_SuppressionSkipsWarn
require.Empty(capture.entries)
}
// Warn о незнакомой location привязан к requestLogEnable как обычный лог запросов:
// при выключенном флаге запрос не логируется, но ответ 404 — без изменений.
func (s *UnknownLocationsLoggingTestSuite) Test_UnknownPath_RequestLoggingDisabled_NoWarn() {
test, require := test.New(s.T())
capture := &logCapture{}
locator := assembly.NewLocator(capture, nil, nil, routes.NewRoutes(test.Logger()), nil, nil, nil, nil, nil)
handler, err := locator.Handler(conf.Remote{Http: conf.Http{MaxRequestBodySizeInMb: 1}}, nil)
require.NoError(err)
srv := httptest.NewServer(handler)
defer srv.Close()
resp, err := httpcli.New().Get(srv.URL + "/unknown/path").Do(s.T().Context())
require.NoError(err)
require.EqualValues(http.StatusNotFound, resp.StatusCode())
body, err := resp.BodyCopy()
require.NoError(err)
require.EqualValues("404 page not found\n", string(body))
require.Empty(capture.entries)
}
func (s *UnknownLocationsLoggingTestSuite) Test_UnknownEndpoint_LogsWarn() {
test, require := test.New(s.T())
capture := &logCapture{}