Skip to content

Commit 0727f4c

Browse files
ENH: Filter out duplicate logs
- 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
1 parent 4b6c6f7 commit 0727f4c

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/daylio_to_md/errors.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,32 @@ def emit(self, record):
3232
self.stream.write(f"{csi}{color}m{formatted_msg}{csi}m\n")
3333

3434

35+
class DuplicateFilter(logging.Filter):
36+
# Class-level attribute to store logged messages
37+
logged_messages = set()
38+
39+
def filter(self, record):
40+
# Create a unique identifier for the log message
41+
current_log = (record.module, record.levelno, record.msg)
42+
43+
if current_log in DuplicateFilter.logged_messages:
44+
return False # Filter out the message if it's a duplicate
45+
46+
DuplicateFilter.logged_messages.add(current_log)
47+
return True # Allow the log through if it's not a duplicate
48+
49+
3550
# Create a console handler for the root logger
3651
# noinspection SpellCheckingInspection
3752
console_log_handler = ColorHandler(sys.stdout)
53+
console_log_handler.addFilter(DuplicateFilter())
3854
# interesting discussion on why setLevel on both handler AND logger: https://stackoverflow.com/a/17668861/8527654
39-
console_log_handler.setLevel(logging.INFO)
4055

56+
console_log_handler.setLevel(logging.INFO)
4157
# noinspection SpellCheckingInspection
42-
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
43-
console_log_handler.setFormatter(formatter)
58+
formatter = logging.Formatter("%(name)s\t%(levelname)s\t%(message)s")
4459

60+
console_log_handler.setFormatter(formatter)
4561
# Add the handlers to the root logger
4662
logging.getLogger().addHandler(console_log_handler)
4763
logging.getLogger().setLevel(logging.INFO)

tests/test_journal_entry.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def test_bare_minimum_journal_entries_as_standalone_class(self):
2525
self.assertIsNone(bare_minimum_entry.note)
2626
self.assertListEqual([], bare_minimum_entry.activities)
2727

28+
@suppress.out
2829
def test_bare_minimum_journal_entries_from_builder_class(self):
2930
# When
3031
bare_minimum_entry = EntryBuilder().build(

0 commit comments

Comments
 (0)