@@ -26,9 +26,11 @@ When to use logging
26
26
^^^^^^^^^^^^^^^^^^^
27
27
28
28
Logging provides a set of convenience functions for simple logging usage. These
29
- are :func: `debug `, :func: `info `, :func: `warning `, :func: `error ` and
30
- :func: `critical `. To determine when to use logging, see the table below, which
31
- states, for each of a set of common tasks, the best tool to use for it.
29
+ can be accessed by creating a logger via ``logger = getLogger(__name__) ``, and
30
+ then calling :func: `logger.debug `, :func: `logger.info `, :func: `logger.warning `,
31
+ :func: `logger.error ` and :func: `logger.critical `. To determine when to use
32
+ logging, see the table below, which states, for each of a set of common tasks,
33
+ the best tool to use for it.
32
34
33
35
+-------------------------------------+--------------------------------------+
34
36
| Task you want to perform | The best tool for the task |
@@ -37,8 +39,8 @@ states, for each of a set of common tasks, the best tool to use for it.
37
39
| usage of a command line script or | |
38
40
| program | |
39
41
+-------------------------------------+--------------------------------------+
40
- | Report events that occur during | :func: `logging .info ` (or |
41
- | normal operation of a program (e.g. | :func: `logging .debug ` for very |
42
+ | Report events that occur during | :func: `logger .info ` (or |
43
+ | normal operation of a program (e.g. | :func: `logger .debug ` for very |
42
44
| for status monitoring or fault | detailed output for diagnostic |
43
45
| investigation) | purposes) |
44
46
+-------------------------------------+--------------------------------------+
@@ -47,22 +49,22 @@ states, for each of a set of common tasks, the best tool to use for it.
47
49
| | the client application should be |
48
50
| | modified to eliminate the warning |
49
51
| | |
50
- | | :func: `logging .warning ` if there is |
52
+ | | :func: `logger .warning ` if there is |
51
53
| | nothing the client application can do|
52
54
| | about the situation, but the event |
53
55
| | should still be noted |
54
56
+-------------------------------------+--------------------------------------+
55
57
| Report an error regarding a | Raise an exception |
56
58
| particular runtime event | |
57
59
+-------------------------------------+--------------------------------------+
58
- | Report suppression of an error | :func: `logging .error `, |
59
- | without raising an exception (e.g. | :func: `logging .exception ` or |
60
- | error handler in a long-running | :func: `logging .critical ` as |
60
+ | Report suppression of an error | :func: `logger .error `, |
61
+ | without raising an exception (e.g. | :func: `logger .exception ` or |
62
+ | error handler in a long-running | :func: `logger .critical ` as |
61
63
| server process) | appropriate for the specific error |
62
64
| | and application domain |
63
65
+-------------------------------------+--------------------------------------+
64
66
65
- The logging functions are named after the level or severity of the events
67
+ The functions are named after the level or severity of the events
66
68
they are used to track. The standard levels and their applicability are
67
69
described below (in increasing order of severity):
68
70
@@ -116,12 +118,18 @@ If you type these lines into a script and run it, you'll see:
116
118
WARNING:root:Watch out!
117
119
118
120
printed out on the console. The ``INFO `` message doesn't appear because the
119
- default level is ``WARNING ``. The printed message includes the indication of
120
- the level and the description of the event provided in the logging call, i.e.
121
- 'Watch out!'. Don't worry about the 'root' part for now: it will be explained
122
- later. The actual output can be formatted quite flexibly if you need that;
123
- formatting options will also be explained later.
124
-
121
+ default level is ``WARNING ``. The printed message includes the indication of the
122
+ level and the description of the event provided in the logging call, i.e.
123
+ 'Watch out!'. The actual output can be formatted quite flexibly if you need
124
+ that; formatting options will also be explained later.
125
+
126
+ Notice that in this example, we use functions directly on the ``logging ``
127
+ module, like ``logging.debug ``, rather than creating a logger and calling
128
+ functions on it. These functions operation on the root logger, but can be useful
129
+ as they will call ``basicConfig `` for you if it has not been called yet, like in
130
+ this example. In larger programs you'll usually want to control this call
131
+ explicitly however, so for that reason as well as others, it's better to explicitly
132
+ create loggers and call their member functions.
125
133
126
134
Logging to a file
127
135
^^^^^^^^^^^^^^^^^
@@ -131,11 +139,12 @@ look at that next. Be sure to try the following in a newly started Python
131
139
interpreter, and don't just continue from the session described above::
132
140
133
141
import logging
142
+ logger = logging.getLogger(__name__)
134
143
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
135
- logging .debug('This message should go to the log file')
136
- logging .info('So should this')
137
- logging .warning('And this, too')
138
- logging .error('And non-ASCII stuff, too, like Øresund and Malmö')
144
+ logger .debug('This message should go to the log file')
145
+ logger .info('So should this')
146
+ logger .warning('And this, too')
147
+ logger .error('And non-ASCII stuff, too, like Øresund and Malmö')
139
148
140
149
.. versionchanged :: 3.9
141
150
The *encoding * argument was added. In earlier Python versions, or if not
@@ -149,10 +158,10 @@ messages:
149
158
150
159
.. code-block :: none
151
160
152
- DEBUG:root :This message should go to the log file
153
- INFO:root :So should this
154
- WARNING:root :And this, too
155
- ERROR:root :And non-ASCII stuff, too, like Øresund and Malmö
161
+ DEBUG:__main__ :This message should go to the log file
162
+ INFO:__main__ :So should this
163
+ WARNING:__main__ :And this, too
164
+ ERROR:__main__ :And non-ASCII stuff, too, like Øresund and Malmö
156
165
157
166
This example also shows how you can set the logging level which acts as the
158
167
threshold for tracking. In this case, because we set the threshold to
@@ -182,10 +191,8 @@ following example::
182
191
logging.basicConfig(level=numeric_level, ...)
183
192
184
193
The call to :func: `basicConfig ` should come *before * any calls to
185
- :func: `debug `, :func: `info `, etc. Otherwise, those functions will call
186
- :func: `basicConfig ` for you with the default options. As it's intended as a
187
- one-off simple configuration facility, only the first call will actually do
188
- anything: subsequent calls are effectively no-ops.
194
+ :func: `logger.debug `, :func: `logger.info `, etc. Otherwise, that logging
195
+ will not be handled.
189
196
190
197
If you run the above script several times, the messages from successive runs
191
198
are appended to the file *example.log *. If you want each run to start afresh,
@@ -198,50 +205,6 @@ The output will be the same as before, but the log file is no longer appended
198
205
to, so the messages from earlier runs are lost.
199
206
200
207
201
- Logging from multiple modules
202
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
203
-
204
- If your program consists of multiple modules, here's an example of how you
205
- could organize logging in it::
206
-
207
- # myapp.py
208
- import logging
209
- import mylib
210
-
211
- def main():
212
- logging.basicConfig(filename='myapp.log', level=logging.INFO)
213
- logging.info('Started')
214
- mylib.do_something()
215
- logging.info('Finished')
216
-
217
- if __name__ == '__main__':
218
- main()
219
-
220
- ::
221
-
222
- # mylib.py
223
- import logging
224
-
225
- def do_something():
226
- logging.info('Doing something')
227
-
228
- If you run *myapp.py *, you should see this in *myapp.log *:
229
-
230
- .. code-block :: none
231
-
232
- INFO:root:Started
233
- INFO:root:Doing something
234
- INFO:root:Finished
235
-
236
- which is hopefully what you were expecting to see. You can generalize this to
237
- multiple modules, using the pattern in *mylib.py *. Note that for this simple
238
- usage pattern, you won't know, by looking in the log file, *where * in your
239
- application your messages came from, apart from looking at the event
240
- description. If you want to track the location of your messages, you'll need
241
- to refer to the documentation beyond the tutorial level -- see
242
- :ref: `logging-advanced-tutorial `.
243
-
244
-
245
208
Logging variable data
246
209
^^^^^^^^^^^^^^^^^^^^^
247
210
0 commit comments