mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
[3.12] gh-115233: Fix an example in the Logging Cookbook (GH-115325) (GH-115355)
Also add more tests for LoggerAdapter.
Also support stacklevel in LoggerAdapter._log().
(cherry picked from commit 91822018ee
)
This commit is contained in:
parent
90d1037046
commit
225856ef3e
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