[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 commit 225856ef3e)
(cherry picked from commit 91822018ee)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Miss Islington (bot) 2024-02-12 22:25:47 +01:00 committed by GitHub
parent d732134232
commit 21edde17b3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 106 additions and 23 deletions

View file

@ -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