Gate unknown location logging on requestLogEnable
This commit is contained in:
1 parent
48af0bb132
commit
1776a6a8e3
3 files changed
+35
-5
No files matched your search
+1
-1
@@ -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
|
||||
}
|
||||
@@ -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),
|
||||
|
||||
@@ -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)
|
||||
@@ -59,7 +62,10 @@ func (s *UnknownLocationsLoggingTestSuite) Test_UnknownPath_SuppressionSkipsWarn
|
||||
|
||||
config := conf.Remote{
|
||||
Http: conf.Http{MaxRequestBodySizeInMb: 1},
|
||||
Logging: conf.Logging{SkipLoggingPathPrefixes: []string{"/unknown"}},
|
||||
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{}
|
||||
|
||||
Reference in new issue
Block a user