Check for json log formatting in log_request

This gets us closer to logging parts of the request
as separate json fields but not all the way there.

For example:

```json
{"written_at": "2020-10-07T21:52:16.975Z", "written_ts": 1602107536975497000, "msg": "{'status': 404, 'method': 'GET', 'ip': '127.0.0.1', 'uri': '/nbextensions/widgets/notebook/js/extension.js', 'request_time': 1.9807815551757812, 'referer': 'http://localhost:8888/notebooks/test.ipynb'}", "type": "log", "logger": "NotebookApp", "thread": "MainThread", "level": "WARNING", "module": "log", "line_no": 54}
```
pull/5799/head
Matt Riedemann 6 years ago
parent 1929fb53b8
commit b617d91052

@ -9,6 +9,10 @@ import json
from tornado.log import access_log
from .prometheus.log_functions import prometheus_log_method
from .utils import enable_json_logs
_enable_json_logs = enable_json_logs()
def log_request(handler):
"""log a bit more information about each request than tornado's default
@ -46,5 +50,11 @@ def log_request(handler):
if status >= 500 and status != 502:
# log all headers if it caused an error
log_method(json.dumps(dict(request.headers), indent=2))
log_method(msg.format(**ns))
if _enable_json_logs:
# FIXME: this still logs the msg as a serialized json string,
# presumably because it's using tornado's access_log rather than
# the logger setup in notebook app with _log_formatter_cls.
log_method(ns)
else:
log_method(msg.format(**ns))
prometheus_log_method(handler)

@ -113,6 +113,7 @@ from notebook._sysinfo import get_sys_info
from ._tz import utcnow, utcfromtimestamp
from .utils import (
check_pid,
enable_json_logs,
pathname2url,
run_sync,
unix_socket_in_use,
@ -139,17 +140,17 @@ except ImportError:
terminado_available = False
# Tolerate missing json_logging package.
enable_json_logs = os.getenv('ENABLE_JSON_LOGGING', 'false').lower() == 'true'
_enable_json_logs = enable_json_logs()
try:
import json_logging
except ImportError:
# If configured for json logs and we can't do it, log a hint.
if enable_json_logs:
if _enable_json_logs:
logging.getLogger(__name__).exception(
'Unable to use json logging due to missing packages. '
'Run "pip install notebook[json-logging]" to fix.'
)
enable_json_logs = False
_enable_json_logs = False
#-----------------------------------------------------------------------------
# Module globals
@ -717,7 +718,7 @@ class NotebookApp(JupyterApp):
# Unless there is a way to re-initialize the log formatter (like with _log_format_changed?)
# we need to load the json logging formatter early here otherwise traitlets complains.
_log_formatter_cls = json_logging.JSONLogFormatter if enable_json_logs else LogFormatter
_log_formatter_cls = json_logging.JSONLogFormatter if _enable_json_logs else LogFormatter
@default('log_level')
def _default_log_level(self):

@ -401,3 +401,7 @@ def unix_socket_in_use(socket_path):
return True
finally:
sock.close()
def enable_json_logs():
return os.getenv('ENABLE_JSON_LOGGING', 'false').lower() == 'true'

Loading…
Cancel
Save