Skip to content

Commit

Permalink
FEAT[log]: Filter out duplicate logs
Browse files Browse the repository at this point in the history
- Filter out logs that are prone to duplicate over the course of processing - such as logs about invalid moods.
- Fix one test fixture not having their logs suppressed
  • Loading branch information
DeutscheGabanna committed Aug 4, 2024
1 parent 1d73980 commit c61ffa6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/daylio_to_md/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,32 @@ def emit(self, record):
self.stream.write(f"{csi}{color}m{formatted_msg}{csi}m\n")


class DuplicateFilter(logging.Filter):
# Class-level attribute to store logged messages
logged_messages = set()

def filter(self, record):
# Create a unique identifier for the log message
current_log = (record.module, record.levelno, record.msg)

if current_log in DuplicateFilter.logged_messages:
return False # Filter out the message if it's a duplicate

DuplicateFilter.logged_messages.add(current_log)
return True # Allow the log through if it's not a duplicate


# Create a console handler for the root logger
# noinspection SpellCheckingInspection
console_log_handler = ColorHandler(sys.stdout)
console_log_handler.addFilter(DuplicateFilter())
# interesting discussion on why setLevel on both handler AND logger: https://stackoverflow.com/a/17668861/8527654
console_log_handler.setLevel(logging.INFO)

console_log_handler.setLevel(logging.INFO)
# noinspection SpellCheckingInspection
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
console_log_handler.setFormatter(formatter)
formatter = logging.Formatter("%(name)s\t%(levelname)s\t%(message)s")

console_log_handler.setFormatter(formatter)
# Add the handlers to the root logger
logging.getLogger().addHandler(console_log_handler)
logging.getLogger().setLevel(logging.INFO)
Expand Down
1 change: 1 addition & 0 deletions tests/test_journal_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def test_bare_minimum_journal_entries_as_standalone_class(self):
self.assertIsNone(bare_minimum_entry.note)
self.assertListEqual([], bare_minimum_entry.activities)

@suppress.out
def test_bare_minimum_journal_entries_from_builder_class(self):
# When
bare_minimum_entry = EntryBuilder().build(
Expand Down

0 comments on commit c61ffa6

Please sign in to comment.