Skip to content

Instantly share code, notes, and snippets.

@mriedem
Created October 9, 2020 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mriedem/7a8226ebd3273cfef611b214e136261a to your computer and use it in GitHub Desktop.
Save mriedem/7a8226ebd3273cfef611b214e136261a to your computer and use it in GitHub Desktop.
diff --git a/notebook/notebookapp.py b/notebook/notebookapp.py
index 45c1cb5eb..a443125fd 100755
--- a/notebook/notebookapp.py
+++ b/notebook/notebookapp.py
@@ -139,19 +139,6 @@ try:
except ImportError:
terminado_available = False
-# Tolerate missing json_logging package.
-_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:
- 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
-
#-----------------------------------------------------------------------------
# Module globals
#-----------------------------------------------------------------------------
@@ -716,9 +703,42 @@ class NotebookApp(JupyterApp):
password=(NotebookPasswordApp, NotebookPasswordApp.description.splitlines()[0]),
)
- # 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 = LogFormatter
+
+ log_format_json = Bool(False, config=True,
+ help=_('Set to True to enable JSON formatted logs. Run '
+ '"pip install notebook[json-logging]" to install the required '
+ 'dependent packages. Can also be set using the '
+ 'environment variable ENABLE_JSON_LOGGING=true.')
+ )
+
+ @default('log_format_json')
+ def _default_log_format_json(self):
+ """Get the log_format_json value from the environment."""
+ return enable_json_logs()
+
+ @validate('log_format_json')
+ def _validate_log_format_json(self, proposal):
+ # See if the `json_logging` package can be imported and, if so,
+ # "tickle" the current `log_format` value after setting the
+ # `_log_formatter_cls`.
+ logger = logging.getLogger(__name__)
+ logger.info('in _validate_log_format_json with proposal: %s', proposal)
+ if proposal['value']: # log_format_json == True
+ try:
+ import json_logging
+ logger.info('changing to JSONLogFormatter')
+ _log_formatter_cls = json_logging.JSONLogFormatter
+ # TODO: should we call json_logging.init_non_web(enable_json=True) here?
+ self.log_format = self.log_format # trigger _log_format_changed
+ except ImportError:
+ # If configured for json logs and we can't do it, log a hint.
+ logger.exception(
+ 'Unable to use json logging due to missing packages. '
+ 'Run "pip install notebook[json-logging]" to fix.'
+ )
+ # Don't actually change the log_format_json value.
+ return proposal['value']
@default('log_level')
def _default_log_level(self):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment