fix(logging): address code review feedback for TaskLogCollector robustness
Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
@@ -25,7 +25,7 @@ class TaskLogCollector(logging.Handler):
|
|||||||
try:
|
try:
|
||||||
msg = self.format(record)
|
msg = self.format(record)
|
||||||
# Extract task_id from messages formatted as "[task_id] ..."
|
# Extract task_id from messages formatted as "[task_id] ..."
|
||||||
if msg and "[" in msg:
|
if msg and "[" in msg and "]" in msg:
|
||||||
start = msg.index("[")
|
start = msg.index("[")
|
||||||
end = msg.index("]", start)
|
end = msg.index("]", start)
|
||||||
task_id = msg[start + 1 : end].strip()
|
task_id = msg[start + 1 : end].strip()
|
||||||
@@ -76,7 +76,7 @@ def log_task_progress(task_id, step_name, status, message=None, file_id=None, de
|
|||||||
If not provided, buffered logger output is used automatically.
|
If not provided, buffered logger output is used automatically.
|
||||||
"""
|
"""
|
||||||
# Auto-capture buffered log output when no explicit detail is given
|
# Auto-capture buffered log output when no explicit detail is given
|
||||||
if detail is None and task_id:
|
if not detail and task_id:
|
||||||
_ensure_collector_installed()
|
_ensure_collector_installed()
|
||||||
collected = _collector.drain(task_id)
|
collected = _collector.drain(task_id)
|
||||||
if collected:
|
if collected:
|
||||||
|
|||||||
@@ -257,3 +257,23 @@ class TestTaskLogCollector:
|
|||||||
assert collector.drain("OK") == ""
|
assert collector.drain("OK") == ""
|
||||||
|
|
||||||
logger.removeHandler(collector)
|
logger.removeHandler(collector)
|
||||||
|
|
||||||
|
def test_collector_handles_malformed_brackets(self):
|
||||||
|
"""Test that the collector handles messages with [ but no ]."""
|
||||||
|
from app.utils.logging import TaskLogCollector
|
||||||
|
|
||||||
|
collector = TaskLogCollector()
|
||||||
|
collector.setFormatter(logging.Formatter("%(message)s"))
|
||||||
|
|
||||||
|
logger = logging.getLogger("test_malformed")
|
||||||
|
logger.addHandler(collector)
|
||||||
|
logger.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
|
logger.info("[no closing bracket")
|
||||||
|
logger.info("no brackets at all")
|
||||||
|
logger.info("")
|
||||||
|
|
||||||
|
# Should not raise and should not buffer anything
|
||||||
|
assert collector.drain("no closing bracket") == ""
|
||||||
|
|
||||||
|
logger.removeHandler(collector)
|
||||||
|
|||||||
Reference in New Issue
Block a user