Skip to content

Commit

Permalink
Merge pull request #2871 from neutrinoceros/filtered_logs
Browse files Browse the repository at this point in the history
Add a filter to ytLogger to avoid duplicated successive entries
  • Loading branch information
munkm committed Sep 11, 2020
2 parents a15bd60 + cbf8b76 commit dd2ec04
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions yt/utilities/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ def set_log_level(level):
ytLogger = logging.getLogger("yt")


class DuplicateFilter(logging.Filter):
"""A filter that removes duplicated successive log entries."""

# source
# https://stackoverflow.com/questions/44691558/suppress-multiple-messages-with-same-content-in-python-logging-module-aka-log-co # noqa
def filter(self, record):
current_log = (record.module, record.levelno, record.msg)
if current_log != getattr(self, "last_log", None):
self.last_log = current_log
return True
return False


ytLogger.addFilter(DuplicateFilter())


def disable_stream_logging():
if len(ytLogger.handlers) > 0:
ytLogger.removeHandler(ytLogger.handlers[0])
Expand Down

0 comments on commit dd2ec04

Please sign in to comment.