Skip to content

Commit 54a954f

Browse files
committed
change logging.py name to resolve naming conflict with logger library
1 parent 1367cb9 commit 54a954f

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

openadapt/custom_logger.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""Module for log message filtering, excluding strings & limiting warnings."""
2+
3+
from collections import defaultdict
4+
import time
5+
6+
from openadapt import config
7+
8+
MESSAGE_TIMESTAMPS = defaultdict(list)
9+
10+
# TODO: move utils.configure_logging to here
11+
12+
13+
def filter_log_messages(data: dict) -> bool:
14+
"""Filter log messages based on the defined criteria.
15+
16+
Args:
17+
data: The log message data from a loguru logger.
18+
19+
Returns:
20+
bool: True if the log message should not be ignored, False otherwise.
21+
"""
22+
# TODO: ultimately, we want to fix the underlying issues, but for now,
23+
# we can ignore these messages
24+
for msg in config.MESSAGES_TO_FILTER:
25+
if msg in data["message"]:
26+
if config.MAX_NUM_WARNINGS_PER_SECOND > 0:
27+
current_timestamp = time.time()
28+
MESSAGE_TIMESTAMPS[msg].append(current_timestamp)
29+
timestamps = MESSAGE_TIMESTAMPS[msg]
30+
31+
# Remove timestamps older than 1 second
32+
timestamps = [
33+
ts
34+
for ts in timestamps
35+
if current_timestamp - ts <= config.WARNING_SUPPRESSION_PERIOD
36+
]
37+
38+
if len(timestamps) > config.MAX_NUM_WARNINGS_PER_SECOND:
39+
return False
40+
41+
MESSAGE_TIMESTAMPS[msg] = timestamps
42+
43+
return True

0 commit comments

Comments
 (0)