@@ -160,8 +160,8 @@ is the module's name in the Python package namespace.
160
160
*msg * using the string formatting operator. (Note that this means that you can
161
161
use keywords in the format string, together with a single dictionary argument.)
162
162
163
- There are three keyword arguments in *kwargs * which are inspected:
164
- *exc_info *, *stack_info *, and *extra *.
163
+ There are four keyword arguments in *kwargs * which are inspected:
164
+ *exc_info *, *stack_info *, * stacklevel * and *extra *.
165
165
166
166
If *exc_info * does not evaluate as false, it causes exception information to be
167
167
added to the logging message. If an exception tuple (in the format returned by
@@ -188,11 +188,19 @@ is the module's name in the Python package namespace.
188
188
This mimics the ``Traceback (most recent call last): `` which is used when
189
189
displaying exception frames.
190
190
191
- The third keyword argument is *extra * which can be used to pass a
192
- dictionary which is used to populate the __dict__ of the LogRecord created for
193
- the logging event with user-defined attributes. These custom attributes can then
194
- be used as you like. For example, they could be incorporated into logged
195
- messages. For example::
191
+ The third optional keyword argument is *stacklevel *, which defaults to ``1 ``.
192
+ If greater than 1, the corresponding number of stack frames are skipped
193
+ when computing the line number and function name set in the :class: `LogRecord `
194
+ created for the logging event. This can be used in logging helpers so that
195
+ the function name, filename and line number recorded are not the information
196
+ for the helper function/method, but rather its caller. The name of this
197
+ parameter mirrors the equivalent one in the :mod: `warnings ` module.
198
+
199
+ The fourth keyword argument is *extra * which can be used to pass a
200
+ dictionary which is used to populate the __dict__ of the :class: `LogRecord `
201
+ created for the logging event with user-defined attributes. These custom
202
+ attributes can then be used as you like. For example, they could be
203
+ incorporated into logged messages. For example::
196
204
197
205
FORMAT = '%(asctime)-15s %(clientip)s %(user)-8s %(message)s'
198
206
logging.basicConfig(format=FORMAT)
@@ -213,9 +221,9 @@ is the module's name in the Python package namespace.
213
221
If you choose to use these attributes in logged messages, you need to exercise
214
222
some care. In the above example, for instance, the :class: `Formatter ` has been
215
223
set up with a format string which expects 'clientip' and 'user' in the attribute
216
- dictionary of the LogRecord. If these are missing, the message will not be
217
- logged because a string formatting exception will occur. So in this case, you
218
- always need to pass the *extra * dictionary with these keys.
224
+ dictionary of the :class: ` LogRecord ` . If these are missing, the message will
225
+ not be logged because a string formatting exception will occur. So in this case,
226
+ you always need to pass the *extra * dictionary with these keys.
219
227
220
228
While this might be annoying, this feature is intended for use in specialized
221
229
circumstances, such as multi-threaded servers where the same code executes in
@@ -230,6 +238,9 @@ is the module's name in the Python package namespace.
230
238
.. versionchanged :: 3.5
231
239
The *exc_info * parameter can now accept exception instances.
232
240
241
+ .. versionadded :: 3.8
242
+ The *stacklevel * parameter was added.
243
+
233
244
234
245
.. method :: Logger.info(msg, *args, **kwargs)
235
246
@@ -300,12 +311,19 @@ is the module's name in the Python package namespace.
300
311
Removes the specified handler *hdlr * from this logger.
301
312
302
313
303
- .. method :: Logger.findCaller(stack_info=False)
314
+ .. method :: Logger.findCaller(stack_info=False, stacklevel=1 )
304
315
305
316
Finds the caller's source filename and line number. Returns the filename, line
306
317
number, function name and stack information as a 4-element tuple. The stack
307
318
information is returned as ``None `` unless *stack_info * is ``True ``.
308
319
320
+ The *stacklevel * parameter is passed from code calling the :meth: `debug `
321
+ and other APIs. If greater than 1, the excess is used to skip stack frames
322
+ before determining the values to be returned. This will generally be useful
323
+ when calling logging APIs from helper/wrapper code, so that the information
324
+ in the event log refers not to the helper/wrapper code, but to the code that
325
+ calls it.
326
+
309
327
310
328
.. method :: Logger.handle(record)
311
329
@@ -646,9 +664,9 @@ sophisticated criteria than levels, they get to see every record which is
646
664
processed by the handler or logger they're attached to: this can be useful if
647
665
you want to do things like counting how many records were processed by a
648
666
particular logger or handler, or adding, changing or removing attributes in
649
- the LogRecord being processed. Obviously changing the LogRecord needs to be
650
- done with some care, but it does allow the injection of contextual information
651
- into logs (see :ref: `filters-contextual `).
667
+ the :class: ` LogRecord ` being processed. Obviously changing the LogRecord needs
668
+ to be done with some care, but it does allow the injection of contextual
669
+ information into logs (see :ref: `filters-contextual `).
652
670
653
671
.. _log-record :
654
672
@@ -702,13 +720,13 @@ wire).
702
720
be used.
703
721
704
722
.. versionchanged :: 3.2
705
- The creation of a `` LogRecord ` ` has been made more configurable by
723
+ The creation of a :class: ` LogRecord ` has been made more configurable by
706
724
providing a factory which is used to create the record. The factory can be
707
725
set using :func: `getLogRecordFactory ` and :func: `setLogRecordFactory `
708
726
(see this for the factory's signature).
709
727
710
728
This functionality can be used to inject your own values into a
711
- LogRecord at creation time. You can use the following pattern::
729
+ :class: ` LogRecord ` at creation time. You can use the following pattern::
712
730
713
731
old_factory = logging.getLogRecordFactory()
714
732