From 1b1d1fd9f7554ed128e87894779f685f22bf7a52 Mon Sep 17 00:00:00 2001 From: Alexander Turenko Date: Mon, 12 Sep 2016 03:01:22 +0300 Subject: [PATCH 1/2] Allow to filter out log messages by templates Fixes #1594. --- docs/settings.rst | 10 ++++++++++ pelican/log.py | 15 +++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/docs/settings.rst b/docs/settings.rst index 6063e7a8..c1da7db6 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -777,6 +777,16 @@ be filtered out. For example: ``[(logging.WARN, 'TAG_SAVE_AS is set to False')]`` +It is possible to filter out messages by a template. Check out source code to +obtain a template. + +For example: ``[(logging.WARN, 'Empty alt attribute for image %s in %s')]`` + +**Warning:** Silencing messages by templates is a dangerous feature. It is +possible to unintentionally filter out multiple message types with the same +template (including messages from future Pelican versions). Proceed with +caution. + .. _reading_only_modified_content: diff --git a/pelican/log.py b/pelican/log.py index 3d365607..907981eb 100644 --- a/pelican/log.py +++ b/pelican/log.py @@ -92,6 +92,7 @@ class LimitFilter(logging.Filter): """ _ignore = set() + _raised_messages = set() _threshold = 5 _group_count = defaultdict(int) @@ -105,12 +106,18 @@ class LimitFilter(logging.Filter): group_args = record.__dict__.get('limit_args', ()) # ignore record if it was already raised - # use .getMessage() and not .msg for string formatting - ignore_key = (record.levelno, record.getMessage()) - if ignore_key in self._ignore: + message_key = (record.levelno, record.getMessage()) + if message_key in self._raised_messages: return False else: - self._ignore.add(ignore_key) + self._raised_messages.add(message_key) + + # ignore LOG_FILTER records + # use .msg and not .getMessage() for string formatting to allow + # filtering by templates + ignore_key = (record.levelno, record.msg) + if ignore_key in self._ignore: + return False # check if we went over threshold if group: From 30b8955134364738edd8d1aae0f91b8cc3865e39 Mon Sep 17 00:00:00 2001 From: Alexander Turenko Date: Mon, 12 Sep 2016 03:02:29 +0300 Subject: [PATCH 2/2] Don't suppress LOG_FILTER messages when --debug passed --- docs/settings.rst | 2 ++ pelican/log.py | 12 ++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/docs/settings.rst b/docs/settings.rst index c1da7db6..6f695f90 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -787,6 +787,8 @@ possible to unintentionally filter out multiple message types with the same template (including messages from future Pelican versions). Proceed with caution. +Note: This option does nothing ``--debug`` is passed. + .. _reading_only_modified_content: diff --git a/pelican/log.py b/pelican/log.py index 907981eb..fe14a29b 100644 --- a/pelican/log.py +++ b/pelican/log.py @@ -112,12 +112,12 @@ class LimitFilter(logging.Filter): else: self._raised_messages.add(message_key) - # ignore LOG_FILTER records - # use .msg and not .getMessage() for string formatting to allow - # filtering by templates - ignore_key = (record.levelno, record.msg) - if ignore_key in self._ignore: - return False + # ignore LOG_FILTER records by templates when "debug" isn't enabled + logger_level = logging.getLogger().getEffectiveLevel() + if logger_level > logging.DEBUG: + ignore_key = (record.levelno, record.msg) + if ignore_key in self._ignore: + return False # check if we went over threshold if group: