Skip to content

Commit 212b590

Browse files
committed
logging: Updated LoggerAdapter implementation.
1 parent c84f016 commit 212b590

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

Lib/logging/__init__.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,64 +1376,64 @@ def process(self, msg, kwargs):
13761376
kwargs["extra"] = self.extra
13771377
return msg, kwargs
13781378

1379+
#
1380+
# Boilerplate convenience methods
1381+
#
13791382
def debug(self, msg, *args, **kwargs):
13801383
"""
1381-
Delegate a debug call to the underlying logger, after adding
1382-
contextual information from this adapter instance.
1384+
Delegate a debug call to the underlying logger.
13831385
"""
1384-
msg, kwargs = self.process(msg, kwargs)
1385-
self.logger.debug(msg, *args, **kwargs)
1386+
self.log(DEBUG, msg, *args, **kwargs)
13861387

13871388
def info(self, msg, *args, **kwargs):
13881389
"""
1389-
Delegate an info call to the underlying logger, after adding
1390-
contextual information from this adapter instance.
1390+
Delegate an info call to the underlying logger.
13911391
"""
1392-
msg, kwargs = self.process(msg, kwargs)
1393-
self.logger.info(msg, *args, **kwargs)
1392+
self.log(INFO, msg, *args, **kwargs)
13941393

13951394
def warning(self, msg, *args, **kwargs):
13961395
"""
1397-
Delegate a warning call to the underlying logger, after adding
1398-
contextual information from this adapter instance.
1396+
Delegate a warning call to the underlying logger.
13991397
"""
1400-
msg, kwargs = self.process(msg, kwargs)
1401-
self.logger.warning(msg, *args, **kwargs)
1398+
self.log(WARNING, msg, *args, **kwargs)
14021399

14031400
warn = warning
14041401

14051402
def error(self, msg, *args, **kwargs):
14061403
"""
1407-
Delegate an error call to the underlying logger, after adding
1408-
contextual information from this adapter instance.
1404+
Delegate an error call to the underlying logger.
14091405
"""
1410-
msg, kwargs = self.process(msg, kwargs)
1411-
self.logger.error(msg, *args, **kwargs)
1406+
self.log(ERROR, msg, *args, **kwargs)
14121407

14131408
def exception(self, msg, *args, **kwargs):
14141409
"""
1415-
Delegate an exception call to the underlying logger, after adding
1416-
contextual information from this adapter instance.
1410+
Delegate an exception call to the underlying logger.
14171411
"""
1418-
msg, kwargs = self.process(msg, kwargs)
14191412
kwargs["exc_info"] = 1
1420-
self.logger.error(msg, *args, **kwargs)
1413+
self.log(ERROR, msg, *args, **kwargs)
14211414

14221415
def critical(self, msg, *args, **kwargs):
14231416
"""
1424-
Delegate a critical call to the underlying logger, after adding
1425-
contextual information from this adapter instance.
1417+
Delegate a critical call to the underlying logger.
14261418
"""
1427-
msg, kwargs = self.process(msg, kwargs)
1428-
self.logger.critical(msg, *args, **kwargs)
1419+
self.log(CRITICAL, msg, *args, **kwargs)
14291420

14301421
def log(self, level, msg, *args, **kwargs):
14311422
"""
14321423
Delegate a log call to the underlying logger, after adding
14331424
contextual information from this adapter instance.
14341425
"""
1435-
msg, kwargs = self.process(msg, kwargs)
1436-
self.logger.log(level, msg, *args, **kwargs)
1426+
if self.isEnabledFor(level):
1427+
msg, kwargs = self.process(msg, kwargs)
1428+
self.logger._log(level, msg, args, **kwargs)
1429+
1430+
def isEnabledFor(self, level):
1431+
"""
1432+
Is this logger enabled for level 'level'?
1433+
"""
1434+
if self.logger.manager.disable >= level:
1435+
return False
1436+
return level >= self.getEffectiveLevel()
14371437

14381438
def setLevel(self, level):
14391439
"""

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ Core and Builtins
5858
Library
5959
-------
6060

61+
- logging: Changed LoggerAdapter implementation internally, to make it
62+
easier to subclass in a useful way.
63+
6164
- logging: hasHandlers method was added to Logger, and isEnabledFor,
6265
getEffectiveLevel, hasHandlers and setLevel were added to LoggerAdapter.
6366
LoggerAdapter was introduced into the unit tests for logging.

0 commit comments

Comments
 (0)