File: configuring-logging.rst

package info (click to toggle)
jupyter-server 2.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,068 kB
  • sloc: python: 21,064; makefile: 186; sh: 25; javascript: 14
file content (143 lines) | stat: -rw-r--r-- 3,832 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
.. _configurable_logging:

Configuring Logging
===================

Jupyter Server (and Jupyter Server extension applications such as Jupyter Lab)
are Traitlets applications.

By default Traitlets applications log to stderr. You can configure them to log
to other locations e.g. log files.

Logging is configured via the ``logging_config`` "trait" which accepts a
:py:func:`logging.config.dictConfig` object. For more information look for
``Application.logging_config`` in :ref:`other-full-config`.


Examples
--------

.. _configurable_logging.jupyter_server:

Jupyter Server
^^^^^^^^^^^^^^

A minimal example which logs Jupyter Server output to a file:

.. code-block:: python

   c.ServerApp.logging_config = {
       "version": 1,
       "handlers": {
           "logfile": {
               "class": "logging.FileHandler",
               "level": "DEBUG",
               "filename": "jupyter_server.log",
           },
       },
       "loggers": {
           "ServerApp": {
               "level": "DEBUG",
               "handlers": ["console", "logfile"],
           },
       },
   }

.. note::

   To keep the default behaviour of logging to stderr ensure the ``console``
   handler (provided by Traitlets) is included in the list of handlers.

.. warning::

   Be aware that the ``ServerApp`` log may contain security tokens. If
   redirecting to log files ensure they have appropriate permissions.


.. _configurable_logging.extension_applications:

Jupyter Server Extension Applications (e.g. Jupyter Lab)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

An example which logs both Jupyter Server and Jupyter Lab output to a file:

.. note::

   Because Jupyter Server and its extension applications are separate Traitlets
   applications their logging must be configured separately.

.. code-block:: python

   c.ServerApp.logging_config = {
       "version": 1,
       "handlers": {
           "logfile": {
               "class": "logging.FileHandler",
               "level": "DEBUG",
               "filename": "jupyter_server.log",
               "formatter": "my_format",
           },
       },
       "formatters": {
           "my_format": {
               "format": "%(asctime)s %(levelname)-8s %(name)-15s %(message)s",
               "datefmt": "%Y-%m-%d %H:%M:%S",
           },
       },
       "loggers": {
           "ServerApp": {
               "level": "DEBUG",
               "handlers": ["console", "logfile"],
           },
       },
   }

   c.LabApp.logging_config = {
       "version": 1,
       "handlers": {
           "logfile": {
               "class": "logging.FileHandler",
               "level": "DEBUG",
               "filename": "jupyter_server.log",
               "formatter": "my_format",
           },
       },
       "formatters": {
           "my_format": {
               "format": "%(asctime)s %(levelname)-8s %(name)-15s %(message)s",
               "datefmt": "%Y-%m-%d %H:%M:%S",
           },
       },
       "loggers": {
           "LabApp": {
               "level": "DEBUG",
               "handlers": ["console", "logfile"],
           },
       },
   }

.. note::

   The configured application name should match the logger name
   e.g. ``c.LabApp.logging_config`` defines a logger called ``LabApp``.

.. tip::

   This diff modifies the example to log Jupyter Server and Jupyter Lab output
   to different files:

   .. code-block:: diff

      --- before
      +++ after
       c.LabApp.logging_config = {
           'version': 1,
           'handlers': {
               'logfile': {
                   'class': 'logging.FileHandler',
                   'level': 'DEBUG',
      -            'filename': 'jupyter_server.log',
      +            'filename': 'jupyter_lab.log',
                   'formatter': 'my_format',
               },
           },