Skip to content

Commit c84f016

Browse files
committed
Added some methods to LoggerAdapter, and updated documentation.
1 parent ceff566 commit c84f016

File tree

5 files changed

+32
-9
lines changed

5 files changed

+32
-9
lines changed

Doc/library/logging.rst

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2599,8 +2599,8 @@ should, then :meth:`flush` is expected to do the needful.
25992599
.. method:: flush()
26002600

26012601
For a :class:`MemoryHandler`, flushing means just sending the buffered
2602-
records to the target, if there is one. Override if you want different
2603-
behavior.
2602+
records to the target, if there is one. The buffer is also cleared when
2603+
this happens. Override if you want different behavior.
26042604

26052605

26062606
.. method:: setTarget(target)
@@ -2972,15 +2972,18 @@ __ context-info_
29722972
'extra'. The return value is a (*msg*, *kwargs*) tuple which has the
29732973
(possibly modified) versions of the arguments passed in.
29742974

2975-
In addition to the above, :class:`LoggerAdapter` supports all the logging
2975+
In addition to the above, :class:`LoggerAdapter` supports the following
29762976
methods of :class:`Logger`, i.e. :meth:`debug`, :meth:`info`, :meth:`warning`,
2977-
:meth:`error`, :meth:`exception`, :meth:`critical` and :meth:`log`. These
2978-
methods have the same signatures as their counterparts in :class:`Logger`, so
2979-
you can use the two types of instances interchangeably.
2977+
:meth:`error`, :meth:`exception`, :meth:`critical`, :meth:`log`,
2978+
:meth:`isEnabledFor`, :meth:`getEffectiveLevel`, :meth:`setLevel`,
2979+
:meth:`hasHandlers`. These methods have the same signatures as their
2980+
counterparts in :class:`Logger`, so you can use the two types of instances
2981+
interchangeably.
29802982

29812983
.. versionchanged:: 3.2
2982-
The :meth:`isEnabledFor` method was added to :class:`LoggerAdapter`. This
2983-
method delegates to the underlying logger.
2984+
The :meth:`isEnabledFor`, :meth:`getEffectiveLevel`, :meth:`setLevel` and
2985+
:meth:`hasHandlers` methods were added to :class:`LoggerAdapter`. These
2986+
methods delegate to the underlying logger.
29842987

29852988

29862989
Thread Safety

Lib/logging/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,6 +1400,8 @@ def warning(self, msg, *args, **kwargs):
14001400
msg, kwargs = self.process(msg, kwargs)
14011401
self.logger.warning(msg, *args, **kwargs)
14021402

1403+
warn = warning
1404+
14031405
def error(self, msg, *args, **kwargs):
14041406
"""
14051407
Delegate an error call to the underlying logger, after adding
@@ -1433,12 +1435,24 @@ def log(self, level, msg, *args, **kwargs):
14331435
msg, kwargs = self.process(msg, kwargs)
14341436
self.logger.log(level, msg, *args, **kwargs)
14351437

1438+
def setLevel(self, level):
1439+
"""
1440+
Set the specified level on the underlying logger.
1441+
"""
1442+
self.logger.setLevel(level)
1443+
14361444
def isEnabledFor(self, level):
14371445
"""
14381446
See if the underlying logger is enabled for the specified level.
14391447
"""
14401448
return self.logger.isEnabledFor(level)
14411449

1450+
def getEffectiveLevel(self):
1451+
"""
1452+
Get the effective level for the underlying logger.
1453+
"""
1454+
return self.logger.getEffectiveLevel()
1455+
14421456
def hasHandlers(self):
14431457
"""
14441458
See if the underlying logger has any handlers.

Lib/logging/handlers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,8 @@ def flush(self):
11311131
For a MemoryHandler, flushing means just sending the buffered
11321132
records to the target, if there is one. Override if you want
11331133
different behaviour.
1134+
1135+
The record buffer is also cleared by this operation.
11341136
"""
11351137
if self.target:
11361138
for record in self.buffer:

Lib/test/test_logging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def test_flat(self):
154154

155155
ERR = logging.getLogger("ERR")
156156
ERR.setLevel(logging.ERROR)
157-
INF = logging.getLogger("INF")
157+
INF = logging.LoggerAdapter(logging.getLogger("INF"), {})
158158
INF.setLevel(logging.INFO)
159159
DEB = logging.getLogger("DEB")
160160
DEB.setLevel(logging.DEBUG)

Misc/NEWS

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

61+
- logging: hasHandlers method was added to Logger, and isEnabledFor,
62+
getEffectiveLevel, hasHandlers and setLevel were added to LoggerAdapter.
63+
LoggerAdapter was introduced into the unit tests for logging.
64+
6165
- Issue #1686: Fix string.Template when overriding the pattern attribute.
6266

6367
- Issue #9854: SocketIO objects now observe the RawIOBase interface in

0 commit comments

Comments
 (0)