diff --git a/notebook/log.py b/notebook/log.py index af87528fe..d81a8ea66 100644 --- a/notebook/log.py +++ b/notebook/log.py @@ -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) diff --git a/notebook/notebookapp.py b/notebook/notebookapp.py index 6c1323edf..45c1cb5eb 100755 --- a/notebook/notebookapp.py +++ b/notebook/notebookapp.py @@ -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): diff --git a/notebook/utils.py b/notebook/utils.py index 1196d6d89..cbb0cf10a 100644 --- a/notebook/utils.py +++ b/notebook/utils.py @@ -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'