4.1 KiB
4.1 KiB
Automation Logging
Purpose
Explain how automation runs are logged, where artifacts are written, and how logs are retrieved via API.
Code Locations (exact paths)
- Logger implementation:
backend/igny8_core/business/automation/services/automation_logger.py - Service usage:
backend/igny8_core/business/automation/services/automation_service.py - API exposure:
backend/igny8_core/business/automation/views.py(logsaction) - Run metadata:
backend/igny8_core/business/automation/models.py
High-Level Responsibilities
- Create per-run directories and log files for automation runs (primary and optional shared mirror).
- Record stage-level start/progress/complete events and structured traces.
- Provide tail retrieval of logs through an API endpoint.
Detailed Behavior
AutomationLogger:- On
start_run: generatesrun_id(run_YYYYMMDD_HHMMSS_<trigger>), creates run directory underbase_log_dir(default/data/app/logs/automation), and optionally mirrors toshared_log_dirwhen configured. Initializesautomation_run.logwith run metadata and writes a JSONL trace entry (run_trace.jsonl). log_stage_start: appends to main log and writes a stage-specific log filestage_<n>.log; mirrors to shared dir if configured; writes a trace event (stage_startwith pending count).log_stage_progress: appends progress lines to main and stage logs (and mirrored copies), and writes astage_progresstrace event.log_stage_complete: writes completion info with processed count, time elapsed, credits used to main/stage logs; emits trace (stage_complete).log_stage_error: writes error lines to logs and emits an error trace.append_trace: helper used by the service for per-item events; appends JSONL entries.get_activity_log: reads and returns the tail ofautomation_run.log(used by API).
- On
AutomationServiceuses the logger at every stage boundary and significant progress point; also logs warnings/errors and mirrors events via the logger’s trace helpers.- Stage result data is stored in
AutomationRun.stage_n_resultJSON fields alongside logs, providing structured state in the DB.
Data Structures / Models Involved (no code)
AutomationRun: stores per-stage result JSON and run status used to correlate with log files.
Execution Flow
- Run start →
start_runcreates directories/files → each stage emits start/progress/complete/error logs and traces → APIlogsendpoint readsautomation_run.logtail for a givenrun_id.
Cross-Module Interactions
- Logging uses the automation run/account/site context; no external modules write to these files. AI stages provide progress data; billing is inferred from AI task logs but not logged here.
State Transitions
- Log files accumulate throughout a run; traces document run lifecycle events. Completion or failure is also reflected in
AutomationRunstatus/results.
Error Handling
- Logger operations are best-effort; exceptions during file/trace writes are swallowed to avoid breaking the run.
- API
logsreturns 404 if the run does not exist; otherwise returns the requested number of lines.
Tenancy Rules
- Run directories are keyed by account/site/run; API access is constrained by site ownership through the viewset query for
AutomationRun.
Billing Rules
- None directly; credit use is recorded in stage result JSON and inferred from AI task logs, not in the file logs.
Background Tasks / Schedulers
- Logging occurs inside Celery-executed stages; no separate scheduled logging tasks.
Key Design Considerations
- File-based logs per run make multi-stage processes auditable and debuggable without DB bloat.
- Optional shared log dir supports centralized aggregation when multiple containers run workers.
- JSONL traces allow machine parsing for UI status feeds while text logs support human debugging.
How Developers Should Work With This Module
- When adding stage steps, emit progress via
AutomationLoggerto keep observability consistent. - Keep log writes best-effort; do not raise from logger helpers.
- Ensure any new API log endpoints read from the same per-run directory structure and remain site/account scoped.