Skip to content

Commit 0eef50d

Browse files
authored
stdlib: Add snake_case shims for isEnabledFor & getEffectiveLevel (#818)
* stdlib: Add snake_case shims for isEnabledFor & getEffectiveLevel Fixes #779 * Add the methods to API docs * Add PR# * Add not about difference
1 parent f194271 commit 0eef50d

4 files changed

Lines changed: 55 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ You can find our backwards-compatibility policy [here](https://github.com/hynek/
4242
This is only available for *structlog*-originated events since the standard library has no equivalent (except for the convention of setting the logger's name to `__name__`).
4343
[#812](https://github.com/hynek/structlog/pull/812)
4444

45+
- `structlog.stdlib.BoundLogger` now has `is_enabled_for()` and `get_effective_level()` methods that are snake_case aliases for its `isEnabledFor()` and `getEffectiveLevel()` methods.
46+
This makes it more compatible with the native `structlog.typing.FilteringBoundLogger`, so you can swap configurations without changing your call sites.
47+
[#818](https://github.com/hynek/structlog/pull/818)
48+
4549

4650
### Changed
4751

‎docs/api.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ API Reference
301301
.. autofunction:: get_logger
302302

303303
.. autoclass:: BoundLogger
304-
:members: bind, unbind, try_unbind, new, debug, info, warning, warn, error, critical, exception, log, adebug, ainfo, awarning, aerror, acritical, aexception, alog
304+
:members: bind, unbind, try_unbind, new, debug, info, warning, warn, error, critical, exception, log, adebug, ainfo, awarning, aerror, acritical, aexception, alog, is_enabled_for, get_effective_level, isEnabledFor, getEffectiveLevel
305305

306306
.. autoclass:: AsyncBoundLogger
307307
:members: sync_bl

‎src/structlog/stdlib.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,31 @@ def isEnabledFor(self, level: int) -> bool:
412412
"""
413413
return self._logger.isEnabledFor(level)
414414

415+
def is_enabled_for(self, level: int) -> bool:
416+
"""
417+
A snake_case alias of `isEnabledFor` for compatibility with
418+
`structlog.typing.FilteringBoundLogger`.
419+
420+
.. note::
421+
422+
This method is more complex than the native `is_enabled_for` since
423+
it supports standard library-only features like
424+
:attr:`logging.Logger.disabled` while the native one only compares
425+
log levels.
426+
427+
.. versionadded:: 26.1.0
428+
"""
429+
return self._logger.isEnabledFor(level)
430+
431+
def get_effective_level(self) -> int:
432+
"""
433+
A snake_case alias of `getEffectiveLevel` for compatibility with
434+
`structlog.typing.FilteringBoundLogger`.
435+
436+
.. versionadded:: 26.1.0
437+
"""
438+
return self._logger.getEffectiveLevel()
439+
415440
def getChild(self, suffix: str) -> logging.Logger:
416441
"""
417442
Calls :meth:`logging.Logger.getChild` with unmodified arguments.

‎tests/test_stdlib.py‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
wrap_logger,
2929
)
3030
from structlog._config import _CONFIG
31-
from structlog._log_levels import CRITICAL, NAME_TO_LEVEL, WARN
31+
from structlog._log_levels import CRITICAL, DEBUG, NAME_TO_LEVEL, WARN
3232
from structlog.dev import ConsoleRenderer
3333
from structlog.exceptions import DropEvent
3434
from structlog.processors import JSONRenderer, KeyValueRenderer
@@ -303,6 +303,30 @@ def validate(*args, **kw):
303303

304304
assert called_stdlib_method[0] is True
305305

306+
def test_is_enabled_for(self):
307+
"""
308+
is_enabled_for is a snake_case alias that delegates to the wrapped
309+
logger's isEnabledFor, for compatibility with FilteringBoundLogger.
310+
"""
311+
stdlib_logger = logging.getLogger("test_is_enabled_for")
312+
stdlib_logger.setLevel(WARN)
313+
bl = BoundLogger(stdlib_logger, [], {})
314+
315+
assert bl.is_enabled_for(WARN) is True
316+
assert bl.is_enabled_for(DEBUG) is False
317+
318+
def test_get_effective_level(self):
319+
"""
320+
get_effective_level is a snake_case alias that delegates to the wrapped
321+
logger's getEffectiveLevel, for compatibility with
322+
FilteringBoundLogger.
323+
"""
324+
stdlib_logger = logging.getLogger("test_get_effective_level")
325+
stdlib_logger.setLevel(WARN)
326+
bl = BoundLogger(stdlib_logger, [], {})
327+
328+
assert WARN == bl.get_effective_level()
329+
306330
def test_exception_exc_info(self):
307331
"""
308332
BoundLogger.exception sets exc_info=True.

0 commit comments

Comments
 (0)