Skip to content

Commit

Permalink
Allow FieldSkipLogFilter to handle exception records
Browse files Browse the repository at this point in the history
Resolves #1035
  • Loading branch information
sgillies committed Feb 1, 2022
1 parent 3a3828a commit 51c80a4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES.txt
Expand Up @@ -14,6 +14,8 @@ Changes:

Bug fixes:

- Allow FieldSkipLogFilter to handle exception messages as well as strings
(reported in #1035).
- Clean up VSI files left by MemoryFileBase, resolving #1041.
- Hard-coded "utf-8" collection encoding added in #423 has been removed
(#1057).
Expand Down
7 changes: 3 additions & 4 deletions fiona/logutils.py
Expand Up @@ -9,14 +9,14 @@ class FieldSkipLogFilter(logging.Filter):
At most, one message per field skipped per loop will be passed.
"""

def __init__(self, name=''):
def __init__(self, name=""):
super(FieldSkipLogFilter, self).__init__(name)
self.seen_msgs = set()

def filter(self, record):
"""Pass record if not seen."""
if getattr(record, 'msg', "").startswith("Skipping field"):
msg = record.getMessage()
msg = record.getMessage()
if msg.startswith("Skipping field"):
retval = msg not in self.seen_msgs
self.seen_msgs.add(msg)
return retval
Expand All @@ -25,7 +25,6 @@ def filter(self, record):


class LogFiltering(object):

def __init__(self, logger, filter):
self.logger = logger
self.filter = filter
Expand Down
11 changes: 11 additions & 0 deletions tests/test_logutils.py
Expand Up @@ -16,6 +16,7 @@ def test_filtering(caplog):
logger.warning("Skipping field 2")
logger.warning("Danger!")
logger.warning("Skipping field 1")

assert len(caplog.records) == 4
assert caplog.records[0].getMessage() == "Attention!"
assert caplog.records[1].getMessage() == "Skipping field 1"
Expand All @@ -39,3 +40,13 @@ def test_skipping_list(caplog, data_dir):
assert len(results) == 3
assert not any(['skip_me' in f['properties'] for f in results])
assert len([rec for rec in caplog.records if rec.getMessage().startswith('Skipping')]) == 1


def test_log_filter_exception(caplog):
"""FieldSkipLogFilter handles exceptions from log.exception()."""
logger = logging.getLogger()
with LogFiltering(logger, FieldSkipLogFilter()):
logger.exception(ValueError("Oh no"))

assert len(caplog.records) == 1
assert caplog.records[0].getMessage() == "Oh no"

0 comments on commit 51c80a4

Please sign in to comment.