From e2199894eb6657703b9b7ad7151d1f180acbf6d9 Mon Sep 17 00:00:00 2001 From: Marat Sharafutdinov Date: Mon, 11 Mar 2019 21:05:31 +0300 Subject: [PATCH] Pass-through stdlib's logger attributes --- src/structlog/stdlib.py | 45 ++++++++++++++++++++++++++++++++++++++++- tests/test_stdlib.py | 15 ++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/structlog/stdlib.py b/src/structlog/stdlib.py index fff4e28c..95ef0fdb 100644 --- a/src/structlog/stdlib.py +++ b/src/structlog/stdlib.py @@ -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. diff --git a/tests/test_stdlib.py b/tests/test_stdlib.py index c02e3f6e..d0209b9e 100644 --- a/tests/test_stdlib.py +++ b/tests/test_stdlib.py @@ -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", [