From c2968ccf393b75e20ae6027cc3c34359e6ed5181 Mon Sep 17 00:00:00 2001 From: Alexander Turenko Date: Wed, 31 Aug 2016 02:18:27 +0300 Subject: [PATCH] Allow to ignore warnings by id --- docs/settings.rst | 8 ++++++++ pelican/log.py | 5 +++++ pelican/readers.py | 3 ++- pelican/settings.py | 4 ++++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/docs/settings.rst b/docs/settings.rst index a0a4b1b2..fbf99b1c 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -106,6 +106,8 @@ Setting name (followed by default value, if any) ``LOG_FILTER = []`` A list of tuples containing the logging level (up to ``warning``) and the message to be ignored. For example: ``[(logging.WARN, 'TAG_SAVE_AS is set to False')]`` +``LOG_FILTER_IDS = []`` A list of containing IDs of messages to be ignored. + For example: ``['warn.img.alt.empty']`` ``READERS = {}`` A dictionary of file extensions / Reader classes for Pelican to process or ignore. For example, to avoid processing .html files, set: ``READERS = {'html': None}``. To add a custom reader for the @@ -774,6 +776,12 @@ be filtered out. For example: ``[(logging.WARN, 'TAG_SAVE_AS is set to False')]`` +It's hard to ignore a group of messages containing dynamic substrings (e.g. +paths to files). Such messages can be ignored using the ``LOG_FILTER_IDS`` +setting. + +For example: ``['warn.img.alt.empty']`` + .. _reading_only_modified_content: diff --git a/pelican/log.py b/pelican/log.py index 3d365607..434f7a3d 100644 --- a/pelican/log.py +++ b/pelican/log.py @@ -92,6 +92,7 @@ class LimitFilter(logging.Filter): """ _ignore = set() + _ignore_ids = set() _threshold = 5 _group_count = defaultdict(int) @@ -100,6 +101,10 @@ class LimitFilter(logging.Filter): if record.levelno > logging.WARN: return True + # filter record if user ignored it by id + if record.__dict__.get('id') in self._ignore_ids: + return False + # extract group group = record.__dict__.get('limit_msg', None) group_args = record.__dict__.get('limit_args', ()) diff --git a/pelican/readers.py b/pelican/readers.py index a3e89af8..0e19a857 100644 --- a/pelican/readers.py +++ b/pelican/readers.py @@ -592,7 +592,8 @@ def find_empty_alt(content, path): logger.warning( 'Empty alt attribute for image %s in %s', os.path.basename(match[1] + match[5]), path, - extra={'limit_msg': 'Other images have empty alt attributes'}) + extra={'limit_msg': 'Other images have empty alt attributes', + 'id': 'warn.img.alt.empty'}) def default_metadata(settings=None, process=None): diff --git a/pelican/settings.py b/pelican/settings.py index 82c50c03..5cff3f1d 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -109,6 +109,7 @@ DEFAULT_CONFIG = { 'JINJA_EXTENSIONS': [], 'JINJA_FILTERS': {}, 'LOG_FILTER': [], + 'LOG_FILTER_IDS': [], 'LOCALE': [''], # defaults to user locale 'DEFAULT_PAGINATION': False, 'DEFAULT_ORPHANS': 0, @@ -223,7 +224,10 @@ def configure_settings(settings): # specify the log messages to be ignored log_filter = settings.get('LOG_FILTER', DEFAULT_CONFIG['LOG_FILTER']) + log_filter_ids = settings.get('LOG_FILTER_IDS', + DEFAULT_CONFIG['LOG_FILTER_IDS']) LimitFilter._ignore.update(set(log_filter)) + LimitFilter._ignore_ids.update(set(log_filter_ids)) # lookup the theme in "pelican/themes" if the given one doesn't exist if not os.path.isdir(settings['THEME']):