From b295d18370bd2eea4b79070b43740384d0b9ad27 Mon Sep 17 00:00:00 2001 From: Cristian Libotean Date: Fri, 24 Apr 2020 01:31:35 +0300 Subject: [PATCH 1/3] Preserve handler filters if present (allows Filter(s) to work with coloredlogs). --- coloredlogs/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/coloredlogs/__init__.py b/coloredlogs/__init__.py index d279180..df2a6f1 100644 --- a/coloredlogs/__init__.py +++ b/coloredlogs/__init__.py @@ -433,8 +433,10 @@ def install(level=None, **kw): if use_colors or use_colors is None: use_colors = terminal_supports_colors(stream) # Create a stream handler. + preserved_filters = handler.filters # preserve any filters the current handler might have handler = logging.StreamHandler(stream) if stream else StandardErrorHandler() handler.setLevel(level) + handler.filters = preserved_filters # and add them back to the new handler # Prepare the arguments to the formatter, allowing the caller to # customize the values of `fmt', `datefmt' and `style' as desired. formatter_options = dict(fmt=kw.get('fmt'), datefmt=kw.get('datefmt')) From 726c4b18d7ee483505154c5a4cf6bbcd38a5f99a Mon Sep 17 00:00:00 2001 From: Cristian Libotean Date: Mon, 27 Apr 2020 17:20:42 +0300 Subject: [PATCH 2/3] Changed the comments to the way the linter wants them --- coloredlogs/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/coloredlogs/__init__.py b/coloredlogs/__init__.py index df2a6f1..39d390a 100644 --- a/coloredlogs/__init__.py +++ b/coloredlogs/__init__.py @@ -433,10 +433,12 @@ def install(level=None, **kw): if use_colors or use_colors is None: use_colors = terminal_supports_colors(stream) # Create a stream handler. - preserved_filters = handler.filters # preserve any filters the current handler might have + # preserve any filters the current handler might have + preserved_filters = handler.filters handler = logging.StreamHandler(stream) if stream else StandardErrorHandler() handler.setLevel(level) - handler.filters = preserved_filters # and add them back to the new handler + # and add them back to the new handler + handler.filters = preserved_filters # Prepare the arguments to the formatter, allowing the caller to # customize the values of `fmt', `datefmt' and `style' as desired. formatter_options = dict(fmt=kw.get('fmt'), datefmt=kw.get('datefmt')) From 95e6c763e3186c89da92c2f8e0813d417c42ecab Mon Sep 17 00:00:00 2001 From: Cristian Libotean Date: Mon, 27 Apr 2020 17:50:46 +0300 Subject: [PATCH 3/3] Fixed problem when handler is None --- coloredlogs/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/coloredlogs/__init__.py b/coloredlogs/__init__.py index 39d390a..5c072d2 100644 --- a/coloredlogs/__init__.py +++ b/coloredlogs/__init__.py @@ -434,11 +434,12 @@ def install(level=None, **kw): use_colors = terminal_supports_colors(stream) # Create a stream handler. # preserve any filters the current handler might have - preserved_filters = handler.filters + preserved_filters = handler.filters if handler else None handler = logging.StreamHandler(stream) if stream else StandardErrorHandler() handler.setLevel(level) # and add them back to the new handler - handler.filters = preserved_filters + if preserved_filters: + handler.filters = preserved_filters # Prepare the arguments to the formatter, allowing the caller to # customize the values of `fmt', `datefmt' and `style' as desired. formatter_options = dict(fmt=kw.get('fmt'), datefmt=kw.get('datefmt'))