|
| 1 | +.. _configurable_logging: |
| 2 | + |
| 3 | +Configuring Logging |
| 4 | +=================== |
| 5 | + |
| 6 | +Jupyter Server (and Jupyter Server extension applications such as Jupyter Lab) |
| 7 | +are Traitlets applications. |
| 8 | + |
| 9 | +By default Traitlets applications log to stderr. You can configure them to log |
| 10 | +to other locations e.g. log files. |
| 11 | + |
| 12 | +Logging is configured via the ``logging_config`` "trait" which accepts a |
| 13 | +:py:func:`logging.config.dictConfig` object. For more information look for |
| 14 | +``Application.logging_config`` in :ref:`other-full-config`. |
| 15 | + |
| 16 | + |
| 17 | +Examples |
| 18 | +-------- |
| 19 | + |
| 20 | +.. _configurable_logging.jupyter_server: |
| 21 | + |
| 22 | +Jupyter Server |
| 23 | +^^^^^^^^^^^^^^ |
| 24 | + |
| 25 | +A minimal example which logs Jupyter Server output to a file: |
| 26 | + |
| 27 | +.. code-block:: python |
| 28 | +
|
| 29 | + c.ServerApp.logging_config = { |
| 30 | + 'version': 1, |
| 31 | + 'handlers': { |
| 32 | + 'logfile': { |
| 33 | + 'class': 'logging.FileHandler', |
| 34 | + 'level': 'DEBUG', |
| 35 | + 'filename': 'jupyter_server.log', |
| 36 | + }, |
| 37 | + }, |
| 38 | + 'loggers': { |
| 39 | + 'ServerApp': { |
| 40 | + 'level': 'DEBUG', |
| 41 | + 'handlers': ['console', 'logfile'], |
| 42 | + }, |
| 43 | + }, |
| 44 | + } |
| 45 | +
|
| 46 | +.. note:: |
| 47 | + |
| 48 | + To keep the default behaviour of logging to stderr ensure the ``console`` |
| 49 | + handler (provided by Traitlets) is included in the list of handlers. |
| 50 | + |
| 51 | +.. warning:: |
| 52 | + |
| 53 | + Be aware that the ``ServerApp`` log may contain security tokens. If |
| 54 | + redirecting to log files ensure they have appropriate permissions. |
| 55 | + |
| 56 | + |
| 57 | +.. _configurable_logging.extension_applications: |
| 58 | + |
| 59 | +Jupyter Server Extension Applications (e.g. Jupyter Lab) |
| 60 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 61 | + |
| 62 | +An example which logs both Jupyter Server and Jupyter Lab output to a file: |
| 63 | + |
| 64 | +.. note:: |
| 65 | + |
| 66 | + Because Jupyter Server and its extension applications are separate Traitlets |
| 67 | + applications their logging must be configured separately. |
| 68 | + |
| 69 | +.. code-block:: python |
| 70 | +
|
| 71 | + c.ServerApp.logging_config = { |
| 72 | + 'version': 1, |
| 73 | + 'handlers': { |
| 74 | + 'logfile': { |
| 75 | + 'class': 'logging.FileHandler', |
| 76 | + 'level': 'DEBUG', |
| 77 | + 'filename': 'jupyter_server.log', |
| 78 | + 'formatter': 'my_format', |
| 79 | + }, |
| 80 | + }, |
| 81 | + 'formatters': { |
| 82 | + 'my_format': { |
| 83 | + 'format': '%(asctime)s %(levelname)-8s %(name)-15s %(message)s', |
| 84 | + 'datefmt': '%Y-%m-%d %H:%M:%S', |
| 85 | + }, |
| 86 | + }, |
| 87 | + 'loggers': { |
| 88 | + 'ServerApp': { |
| 89 | + 'level': 'DEBUG', |
| 90 | + 'handlers': ['console', 'logfile'], |
| 91 | + }, |
| 92 | + }, |
| 93 | + } |
| 94 | +
|
| 95 | + c.LabApp.logging_config = { |
| 96 | + 'version': 1, |
| 97 | + 'handlers': { |
| 98 | + 'logfile': { |
| 99 | + 'class': 'logging.FileHandler', |
| 100 | + 'level': 'DEBUG', |
| 101 | + 'filename': 'jupyter_server.log', |
| 102 | + 'formatter': 'my_format', |
| 103 | + }, |
| 104 | + }, |
| 105 | + 'formatters': { |
| 106 | + 'my_format': { |
| 107 | + 'format': '%(asctime)s %(levelname)-8s %(name)-15s %(message)s', |
| 108 | + 'datefmt': '%Y-%m-%d %H:%M:%S', |
| 109 | + }, |
| 110 | + }, |
| 111 | + 'loggers': { |
| 112 | + 'LabApp': { |
| 113 | + 'level': 'DEBUG', |
| 114 | + 'handlers': ['console', 'logfile'], |
| 115 | + }, |
| 116 | + }, |
| 117 | + } |
| 118 | +
|
| 119 | +.. note:: |
| 120 | + |
| 121 | + The configured application name should match the logger name |
| 122 | + e.g. ``c.LabApp.logging_config`` defines a logger called ``LabApp``. |
| 123 | + |
| 124 | +.. tip:: |
| 125 | + |
| 126 | + This diff modifies the example to log Jupyter Server and Jupyter Lab output |
| 127 | + to different files: |
| 128 | + |
| 129 | + .. code-block:: diff |
| 130 | +
|
| 131 | + --- before |
| 132 | + +++ after |
| 133 | + c.LabApp.logging_config = { |
| 134 | + 'version': 1, |
| 135 | + 'handlers': { |
| 136 | + 'logfile': { |
| 137 | + 'class': 'logging.FileHandler', |
| 138 | + 'level': 'DEBUG', |
| 139 | + - 'filename': 'jupyter_server.log', |
| 140 | + + 'filename': 'jupyter_lab.log', |
| 141 | + 'formatter': 'my_format', |
| 142 | + }, |
| 143 | + }, |
0 commit comments