Allow to ignore warnings by id

This commit is contained in:
Alexander Turenko 2016-08-31 02:18:27 +03:00
commit c2968ccf39
4 changed files with 19 additions and 1 deletions

View file

@ -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:

View file

@ -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', ())

View file

@ -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):

View file

@ -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']):