Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass-through stdlib's logger attributes #198

Merged
merged 1 commit into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion src/structlog/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,52 @@ def _proxy_to_logger(self, method_name, event, *event_args, **event_kw):
)

#
# Pass-through methods to mimick the stdlib's logger interface.
# Pass-through attributes and methods to mimick the stdlib's logger
# interface.
#

@property
def name(self):
"""
Returns :attr:`logging.Logger.name`
"""
return self._logger.name

@property
def level(self):
"""
Returns :attr:`logging.Logger.level`
"""
return self._logger.level

@property
def parent(self):
"""
Returns :attr:`logging.Logger.parent`
"""
return self._logger.parent

@property
def propagate(self):
"""
Returns :attr:`logging.Logger.propagate`
"""
return self._logger.propagate

@property
def handlers(self):
"""
Returns :attr:`logging.Logger.handlers`
"""
return self._logger.handlers

@property
def disabled(self):
"""
Returns :attr:`logging.Logger.disabled`
"""
return self._logger.disabled

def setLevel(self, level):
"""
Calls :meth:`logging.Logger.setLevel` with unmodified arguments.
Expand Down
15 changes: 15 additions & 0 deletions tests/test_stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,21 @@ def test_positional_args_proxied(self):
assert "baz" == kwargs.get("bar")
assert ("foo",) == kwargs.get("positional_args")

@pytest.mark.parametrize(
"attribute_name",
["name", "level", "parent", "propagate", "handlers", "disabled"],
)
def test_stdlib_passthrough_attributes(self, attribute_name):
"""
stdlib logger attributes are also available in stdlib BoundLogger.
"""
stdlib_logger = logging.getLogger("Test")
stdlib_logger_attribute = getattr(stdlib_logger, attribute_name)
bl = BoundLogger(stdlib_logger, [], {})
bound_logger_attribute = getattr(bl, attribute_name)

assert bound_logger_attribute == stdlib_logger_attribute

@pytest.mark.parametrize(
"method_name,method_args",
[
Expand Down