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