From 09caa44b0bdd39390ac0d20e72926344f20cc20a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20H=E1=BB=93ng=20Qu=C3=A2n?= Date: Thu, 1 Aug 2019 17:19:48 +0700 Subject: [PATCH 1/2] Respect configuration of specific logger when redirecting to std logging --- .gitignore | 1 + AUTHORS | 2 +- CHANGES | 7 ++--- logbook/compat.py | 12 ++++++++- tests/test_logging_compat.py | 52 ++++++++++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 9e999173..0b55b94c 100644 --- a/.gitignore +++ b/.gitignore @@ -65,4 +65,5 @@ env* .vagrant flycheck-* .idea +.vscode .python-version diff --git a/AUTHORS b/AUTHORS index 46b53785..50e46f47 100644 --- a/AUTHORS +++ b/AUTHORS @@ -17,4 +17,4 @@ Contributors: - Raphaël Vinot - Rotem Yaari - Frazer McLean - +- Nguyễn Hồng Quân diff --git a/CHANGES b/CHANGES index 250ee450..e182a2ff 100644 --- a/CHANGES +++ b/CHANGES @@ -1,10 +1,13 @@ Logbook Changelog ================= +Here you can see the full list of changes between each Logbook release. + Version 1.5.0 ------------- - Added support for asyncio +- Respect configuration of specific logger in compatibility mode Version 1.4.3 ------------- @@ -22,8 +25,6 @@ Released on December 11th, 2018 - Try to reconnect to SyslogHandler TCP sockets when they are disconnected (thanks Jonathan Kamens) - Use RFC 5424 format for networking logging in SyslogHandler (thanks Jonathan Kamens) -Here you can see the full list of changes between each Logbook release. - Version 1.4.1 ------------- @@ -72,7 +73,7 @@ Version 1.0.1 - Fix PushOver handler cropping (thanks Sébastien Celles) -VERSION 1.0.0 +Version 1.0.0 ------------- Released on June 26th 2016 diff --git a/logbook/compat.py b/logbook/compat.py index b65ac006..3866fe33 100644 --- a/logbook/compat.py +++ b/logbook/compat.py @@ -172,12 +172,22 @@ def __init__(self, logger=None, level=logbook.NOTSET, filter=None, elif isinstance(logger, string_types): logger = logging.getLogger(logger) self.logger = logger + # Cache loggers of specific name + self.sublogs = {} def get_logger(self, record): """Returns the logger to use for this record. This implementation always return :attr:`logger`. """ - return self.logger + name = record.channel + if name == self.logger.name or not name: + return self.logger + # Look up in cache + logger = self.sublogs.get(name) + if not logger: + logger = logging.getLogger(name) + self.sublogs[name] = logger + return logger def convert_level(self, level): """Converts a logbook level into a logging level.""" diff --git a/tests/test_logging_compat.py b/tests/test_logging_compat.py index 7964993c..9cc3ff98 100644 --- a/tests/test_logging_compat.py +++ b/tests/test_logging_compat.py @@ -67,6 +67,58 @@ def test_redirect_logbook(): logger.handlers[:] = old_handlers +def test_redirect_logbook_respect_specific_configuration(): + import logging + import logging.config + out1 = StringIO() + out2 = StringIO() + config = { + 'version': 1, + 'formatters': { + 'brief': { + 'format': '%(name)s:%(levelname)s:%(message)s' + } + }, + 'handlers': { + 'console_1': { + 'class': 'logging.StreamHandler', + 'stream': out1, + 'level': 'INFO', + }, + 'console_2': { + 'class': 'logging.StreamHandler', + 'stream': out2, + 'level': 'INFO', + 'formatter': 'brief' + }, + }, + 'root': { + 'level': 'INFO', + 'handlers': ['console_1'], + }, + 'loggers': { + 'module_2': { + 'handlers': ['console_2'], + 'propagate': False + } + }, + } + logger = logging.getLogger() + logbook_logger = logbook.Logger('module_2') + old_handlers = logger.handlers[:] + logging.config.dictConfig(config) + try: + with logbook.compat.LoggingHandler(): + logbook_logger.warn("This goes to logging") + pieces = out2.getvalue().strip().split(':') + # Check if our message goes to console_2 + assert pieces == ['module_2', 'WARNING', 'This goes to logging'] + # Check that our message doesn't go to console_1 + assert out1.getvalue() == '' + finally: + logger.handlers[:] = old_handlers + + from itertools import count test_warning_redirections_i = count() From 53dc3560847e3b4d5908489db501e4ab3ae51002 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20H=E1=BB=93ng=20Qu=C3=A2n?= Date: Thu, 1 Aug 2019 18:00:44 +0700 Subject: [PATCH 2/2] Don't damage other test cases. --- tests/test_logging_compat.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_logging_compat.py b/tests/test_logging_compat.py index 9cc3ff98..1ebbf16c 100644 --- a/tests/test_logging_compat.py +++ b/tests/test_logging_compat.py @@ -74,6 +74,7 @@ def test_redirect_logbook_respect_specific_configuration(): out2 = StringIO() config = { 'version': 1, + 'disable_existing_loggers': False, 'formatters': { 'brief': { 'format': '%(name)s:%(levelname)s:%(message)s'