diff --git a/assembly/locator.go b/assembly/locator.go index 66ed355..f21890c 100644 --- a/assembly/locator.go +++ b/assembly/locator.go @@ -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 } diff --git a/middleware/unknown_location.go b/middleware/unknown_location.go index ac8a020..4c714b4 100644 --- a/middleware/unknown_location.go +++ b/middleware/unknown_location.go @@ -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), diff --git a/tests/unknown_locations_logging_test.go b/tests/unknown_locations_logging_test.go index 329f67f..2f62e1c 100644 --- a/tests/unknown_locations_logging_test.go +++ b/tests/unknown_locations_logging_test.go @@ -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{}