mirror of
https://github.com/python/cpython.git
synced 2025-08-15 22:30:42 +00:00
[3.11] gh-115233: Fix an example in the Logging Cookbook (GH-115325) (GH-115355) (GH-115357)
Also add more tests for LoggerAdapter. Also support stacklevel in LoggerAdapter._log(). (cherry picked from commit225856ef3e
) (cherry picked from commit91822018ee
) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
d732134232
commit
21edde17b3
4 changed files with 106 additions and 23 deletions
|
@ -1744,13 +1744,11 @@ to the above, as in the following example::
|
|||
return self.fmt.format(*self.args)
|
||||
|
||||
class StyleAdapter(logging.LoggerAdapter):
|
||||
def __init__(self, logger, extra=None):
|
||||
super().__init__(logger, extra or {})
|
||||
|
||||
def log(self, level, msg, /, *args, **kwargs):
|
||||
def log(self, level, msg, /, *args, stacklevel=1, **kwargs):
|
||||
if self.isEnabledFor(level):
|
||||
msg, kwargs = self.process(msg, kwargs)
|
||||
self.logger._log(level, Message(msg, args), (), **kwargs)
|
||||
self.logger.log(level, Message(msg, args), **kwargs,
|
||||
stacklevel=stacklevel+1)
|
||||
|
||||
logger = StyleAdapter(logging.getLogger(__name__))
|
||||
|
||||
|
@ -1762,7 +1760,7 @@ to the above, as in the following example::
|
|||
main()
|
||||
|
||||
The above script should log the message ``Hello, world!`` when run with
|
||||
Python 3.2 or later.
|
||||
Python 3.8 or later.
|
||||
|
||||
|
||||
.. currentmodule:: logging
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue