always log slow requests at least at info-level

if health or static responses are taking longer than 1s, it's useful to know
This commit is contained in:
Min RK
2021-01-20 09:23:26 +01:00
parent adb5f6ab2a
commit 202d6f93d4

View File

@@ -2,7 +2,9 @@
# Copyright (c) Jupyter Development Team. # Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License. # Distributed under the terms of the Modified BSD License.
import json import json
import logging
import traceback import traceback
from functools import partial
from http.cookies import SimpleCookie from http.cookies import SimpleCookie
from urllib.parse import urlparse from urllib.parse import urlparse
from urllib.parse import urlunparse from urllib.parse import urlunparse
@@ -132,19 +134,25 @@ def log_request(handler):
status < 300 and isinstance(handler, (StaticFileHandler, HealthCheckHandler)) status < 300 and isinstance(handler, (StaticFileHandler, HealthCheckHandler))
): ):
# static-file success and 304 Found are debug-level # static-file success and 304 Found are debug-level
log_method = access_log.debug log_level = logging.DEBUG
elif status < 400: elif status < 400:
log_method = access_log.info log_level = logging.INFO
elif status < 500: elif status < 500:
log_method = access_log.warning log_level = logging.WARNING
else: else:
log_method = access_log.error log_level = logging.ERROR
uri = _scrub_uri(request.uri) uri = _scrub_uri(request.uri)
headers = _scrub_headers(request.headers) headers = _scrub_headers(request.headers)
request_time = 1000.0 * handler.request.request_time() request_time = 1000.0 * handler.request.request_time()
# always log slow responses (longer than 1s) at least info-level
if request_time >= 1000 and log_level < logging.INFO:
log_level = logging.INFO
log_method = partial(access_log.log, log_level)
try: try:
user = handler.current_user user = handler.current_user
except (HTTPError, RuntimeError): except (HTTPError, RuntimeError):