@@ -25,10 +25,12 @@ or *severity*.
25
25
When to use logging
26
26
^^^^^^^^^^^^^^^^^^^
27
27
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.
28
+ You can access logging functionality by creating a logger via ``logger =
29
+ getLogger(__name__) ``, and then calling the logger's :meth: `~Logger.debug `,
30
+ :meth: `~Logger.info `, :meth: `~Logger.warning `, :meth: `~Logger.error ` and
31
+ :meth: `~Logger.critical ` methods. To determine when to use logging, and to see
32
+ which logger methods to use when, see the table below. It states, for each of a
33
+ set of common tasks, the best tool to use for that task.
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 | A logger's :meth: ` ~Logger .info ` (or |
43
+ | normal operation of a program (e.g. | :meth:`~Logger .debug` method for very|
42
44
| for status monitoring or fault | detailed output for diagnostic |
43
45
| investigation) | purposes) |
44
46
+-------------------------------------+--------------------------------------+
@@ -47,22 +49,23 @@ 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 |
51
- | | nothing the client application can do|
52
- | | about the situation, but the event |
53
- | | should still be noted |
52
+ | | A logger's :meth: `~Logger.warning ` |
53
+ | | method if there is nothing the client|
54
+ | | application can do about the |
55
+ | | situation, but the event should still|
56
+ | | be noted |
54
57
+-------------------------------------+--------------------------------------+
55
58
| Report an error regarding a | Raise an exception |
56
59
| particular runtime event | |
57
60
+-------------------------------------+--------------------------------------+
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 |
61
+ | Report suppression of an error | A logger's :meth: ` ~Logger .error `, |
62
+ | without raising an exception (e.g. | :meth: ` ~Logger .exception ` or |
63
+ | error handler in a long-running | :meth: ` ~Logger .critical ` method as |
61
64
| server process) | appropriate for the specific error |
62
65
| | and application domain |
63
66
+-------------------------------------+--------------------------------------+
64
67
65
- The logging functions are named after the level or severity of the events
68
+ The logger methods are named after the level or severity of the events
66
69
they are used to track. The standard levels and their applicability are
67
70
described below (in increasing order of severity):
68
71
@@ -116,12 +119,18 @@ If you type these lines into a script and run it, you'll see:
116
119
WARNING:root:Watch out!
117
120
118
121
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
-
122
+ default level is ``WARNING ``. The printed message includes the indication of the
123
+ level and the description of the event provided in the logging call, i.e.
124
+ 'Watch out!'. The actual output can be formatted quite flexibly if you need
125
+ that; formatting options will also be explained later.
126
+
127
+ Notice that in this example, we use functions directly on the ``logging ``
128
+ module, like ``logging.debug ``, rather than creating a logger and calling
129
+ functions on it. These functions operation on the root logger, but can be useful
130
+ as they will call :func: `~logging.basicConfig ` for you if it has not been called yet, like in
131
+ this example. In larger programs you'll usually want to control the logging
132
+ configuration explicitly however - so for that reason as well as others, it's
133
+ better to create loggers and call their methods.
125
134
126
135
Logging to a file
127
136
^^^^^^^^^^^^^^^^^
@@ -131,11 +140,12 @@ look at that next. Be sure to try the following in a newly started Python
131
140
interpreter, and don't just continue from the session described above::
132
141
133
142
import logging
143
+ logger = logging.getLogger(__name__)
134
144
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ö')
145
+ logger .debug('This message should go to the log file')
146
+ logger .info('So should this')
147
+ logger .warning('And this, too')
148
+ logger .error('And non-ASCII stuff, too, like Øresund and Malmö')
139
149
140
150
.. versionchanged :: 3.9
141
151
The *encoding * argument was added. In earlier Python versions, or if not
@@ -149,10 +159,10 @@ messages:
149
159
150
160
.. code-block :: none
151
161
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ö
162
+ DEBUG:__main__ :This message should go to the log file
163
+ INFO:__main__ :So should this
164
+ WARNING:__main__ :And this, too
165
+ ERROR:__main__ :And non-ASCII stuff, too, like Øresund and Malmö
156
166
157
167
This example also shows how you can set the logging level which acts as the
158
168
threshold for tracking. In this case, because we set the threshold to
@@ -181,11 +191,9 @@ following example::
181
191
raise ValueError('Invalid log level: %s' % loglevel)
182
192
logging.basicConfig(level=numeric_level, ...)
183
193
184
- 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
+ The call to :func: `basicConfig ` should come *before * any calls to a logger's
195
+ methods such as :meth: `~Logger.debug `, :meth: `~Logger.info `, etc. Otherwise,
196
+ that logging event may not be handled in the desired manner.
189
197
190
198
If you run the above script several times, the messages from successive runs
191
199
are appended to the file *example.log *. If you want each run to start afresh,
@@ -198,50 +206,6 @@ The output will be the same as before, but the log file is no longer appended
198
206
to, so the messages from earlier runs are lost.
199
207
200
208
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
209
Logging variable data
246
210
^^^^^^^^^^^^^^^^^^^^^
247
211
0 commit comments