Skip to content

Commit cf1a782

Browse files
authored
fix(utils): prevent duplicate log messages (#339) (#366)
* fix(utils): prevent duplicate log messages (#339) Acquire a lock in utils.configure_logging. Otherwise this function has a race where two threads can both call logger.remove(), and then both call logger.add(), creating two identical sinks. * refactor(record): remove redundant configure_logging calls configure_logging is already called at the module level so there's no need to call it with identical arguments in various functions. * fixup! fix(utils): prevent duplicate log messages (#339)
1 parent 0ee5397 commit cf1a782

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

openadapt/record.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ def process_events(
134134
terminate_event: An event to signal the termination of the process.
135135
"""
136136

137-
utils.configure_logging(logger, LOG_LEVEL)
138137
utils.set_start_time(recording_timestamp)
139138
logger.info(f"starting")
140139

@@ -278,7 +277,6 @@ def write_events(
278277
the number of events left to be written.
279278
"""
280279

281-
utils.configure_logging(logger, LOG_LEVEL)
282280
utils.set_start_time(recording_timestamp)
283281
logger.info(f"{event_type=} starting")
284282
signal.signal(signal.SIGINT, signal.SIG_IGN)
@@ -436,7 +434,6 @@ def read_screen_events(
436434
recording_timestamp: The timestamp of the recording.
437435
"""
438436

439-
utils.configure_logging(logger, LOG_LEVEL)
440437
utils.set_start_time(recording_timestamp)
441438
logger.info(f"starting")
442439
while not terminate_event.is_set():
@@ -463,7 +460,6 @@ def read_window_events(
463460
recording_timestamp: The timestamp of the recording.
464461
"""
465462

466-
utils.configure_logging(logger, LOG_LEVEL)
467463
utils.set_start_time(recording_timestamp)
468464
logger.info(f"starting")
469465
prev_window_data = {}
@@ -513,7 +509,6 @@ def performance_stats_writer(
513509
terminate_event: An event to signal the termination of the process.
514510
"""
515511

516-
utils.configure_logging(logger, LOG_LEVEL)
517512
utils.set_start_time(recording_timestamp)
518513
logger.info("performance stats writer starting")
519514
signal.signal(signal.SIGINT, signal.SIG_IGN)
@@ -535,7 +530,6 @@ def performance_stats_writer(
535530
def memory_writer(
536531
recording_timestamp: float, terminate_event: multiprocessing.Event, record_pid: int
537532
):
538-
utils.configure_logging(logger, LOG_LEVEL)
539533
utils.set_start_time(recording_timestamp)
540534
logger.info("Memory writer starting")
541535
signal.signal(signal.SIGINT, signal.SIG_IGN)
@@ -689,7 +683,6 @@ def record(
689683
task_description: a text description of the task that will be recorded
690684
"""
691685

692-
utils.configure_logging(logger, LOG_LEVEL)
693686
logger.info(f"{task_description=}")
694687

695688
recording = create_recording(task_description)

openadapt/utils.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import inspect
77
import os
88
import sys
9+
import threading
910
import time
1011

1112
from loguru import logger
@@ -20,27 +21,31 @@
2021

2122
EMPTY = (None, [], {}, "")
2223

24+
_logger_lock = threading.Lock()
25+
2326

2427
def configure_logging(logger, log_level):
2528
# TODO: redact log messages (https://github.com/Delgan/loguru/issues/17#issuecomment-717526130)
2629
log_level_override = os.getenv("LOG_LEVEL")
2730
log_level = log_level_override or log_level
28-
logger.remove()
29-
logger_format = (
30-
"<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | "
31-
"<level>{level: <8}</level> | "
32-
"<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> "
33-
"- <level>{message}</level>"
34-
)
35-
logger.add(
36-
StreamHandler(sys.stderr),
37-
colorize=True,
38-
level=log_level,
39-
enqueue=True,
40-
format=logger_format,
41-
filter=config.filter_log_messages if config.IGNORE_WARNINGS else None,
42-
)
43-
logger.debug(f"{log_level=}")
31+
32+
with _logger_lock:
33+
logger.remove()
34+
logger_format = (
35+
"<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | "
36+
"<level>{level: <8}</level> | "
37+
"<cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> "
38+
"- <level>{message}</level>"
39+
)
40+
logger.add(
41+
StreamHandler(sys.stderr),
42+
colorize=True,
43+
level=log_level,
44+
enqueue=True,
45+
format=logger_format,
46+
filter=config.filter_log_messages if config.IGNORE_WARNINGS else None,
47+
)
48+
logger.debug(f"{log_level=}")
4449

4550

4651
def row2dict(row, follow=True):

0 commit comments

Comments
 (0)