add heavy loggin
This commit is contained in:
@@ -6,6 +6,9 @@ Stages call these instead of constructing dicts or dataclasses directly.
|
||||
|
||||
Run context (run_id, parent_job_id) is set once at pipeline start via
|
||||
set_run_context() and automatically injected into all events.
|
||||
|
||||
Log level is set per-run with optional per-stage overrides.
|
||||
DEBUG events are only pushed when the run (or stage) log level allows it.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -16,23 +19,53 @@ from datetime import datetime, timezone
|
||||
from detect.events import push_detect_event
|
||||
from detect.models import PipelineStats
|
||||
|
||||
# Log level ordering for comparison
|
||||
_LEVEL_ORDER = {"DEBUG": 0, "INFO": 1, "WARN": 2, "ERROR": 3}
|
||||
|
||||
# Module-level run context — set once per pipeline invocation
|
||||
_run_context: dict = {}
|
||||
_run_log_level: str = "INFO"
|
||||
_stage_log_levels: dict[str, str] = {} # stage_name → level override
|
||||
|
||||
|
||||
def set_run_context(run_id: str = "", parent_job_id: str = "", run_type: str = "initial"):
|
||||
def set_run_context(
|
||||
run_id: str = "",
|
||||
parent_job_id: str = "",
|
||||
run_type: str = "initial",
|
||||
log_level: str = "INFO",
|
||||
):
|
||||
"""Set the run context for all subsequent events in this pipeline invocation."""
|
||||
global _run_context
|
||||
global _run_context, _run_log_level
|
||||
_run_context = {
|
||||
"run_id": run_id,
|
||||
"parent_job_id": parent_job_id,
|
||||
"run_type": run_type,
|
||||
}
|
||||
_run_log_level = log_level.upper()
|
||||
_stage_log_levels.clear()
|
||||
|
||||
|
||||
def set_stage_log_level(stage: str, level: str):
|
||||
"""Override log level for a specific stage."""
|
||||
_stage_log_levels[stage] = level.upper()
|
||||
|
||||
|
||||
def clear_stage_log_level(stage: str):
|
||||
"""Remove per-stage log level override."""
|
||||
_stage_log_levels.pop(stage, None)
|
||||
|
||||
|
||||
def clear_run_context():
|
||||
global _run_context
|
||||
global _run_context, _run_log_level
|
||||
_run_context = {}
|
||||
_run_log_level = "INFO"
|
||||
_stage_log_levels.clear()
|
||||
|
||||
|
||||
def _should_emit(level: str, stage: str) -> bool:
|
||||
"""Check if this log level should be emitted given run/stage settings."""
|
||||
effective = _stage_log_levels.get(stage, _run_log_level)
|
||||
return _LEVEL_ORDER.get(level.upper(), 1) >= _LEVEL_ORDER.get(effective, 1)
|
||||
|
||||
|
||||
def _inject_context(payload: dict) -> dict:
|
||||
@@ -45,6 +78,8 @@ def _inject_context(payload: dict) -> dict:
|
||||
def log(job_id: str | None, stage: str, level: str, msg: str) -> None:
|
||||
if not job_id:
|
||||
return
|
||||
if not _should_emit(level, stage):
|
||||
return
|
||||
payload = {
|
||||
"level": level,
|
||||
"stage": stage,
|
||||
|
||||
Reference in New Issue
Block a user