feat(backend): daily request-tagged log files + print job lifecycle logging

Platform v2 step 0.4 (APPS_PLATFORM §10). New core/logging.py:
- root logger -> stderr + <db dir>/logs/xenia-YYYY-MM-DD.log (UTC date,
  /app/data/logs in Docker, $LOG_DIR overrides); files older than the
  system.log_retention_days setting (default 30) are pruned
- line format: UTC ISO time, level, logger, [req= user= device=], message
- RequestContextMiddleware (pure ASGI): request id per request, X-Request-ID
  response header, one http line per request (DEBUG for plain reads)
- auth tags the user via set_log_context; uvicorn loggers routed through ours
Print jobs now log created / attempt / success / failed / abandoned with the
job id, so a missing ticket can be traced. Removed cloud_sync's basicConfig.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-12 16:31:38 +02:00
co-authored by Claude Opus 5.5
parent e560d93cd5
commit 9fa728c039
9 changed files with 432 additions and 7 deletions
+18 -3
View File
@@ -1924,9 +1924,13 @@ def _do_route_and_print(order_id: int, item_ids: List[int], db: Session, ephemer
)
db.add(pj)
db.flush()
logger.info("print.job.created job=%s order=%s printer=%s zone=%s items=%s copies=%s",
pj.id, order_id, printer.name, zone_id or "-", item_ids_json, copies)
pj.last_attempted_at = now
pj.retry_count = (pj.retry_count or 0) + 1
logger.info("print.job.attempt job=%s attempt=%s printer=%s addr=%s:%s",
pj.id, pj.retry_count, printer.name, printer.ip_address, printer.port)
success = False
error_msg = None
@@ -1940,9 +1944,11 @@ def _do_route_and_print(order_id: int, item_ids: List[int], db: Session, ephemer
pj.succeeded_at = now
for item in job_items:
printed_item_ids.add(item.id)
logger.info("print.job.success job=%s attempt=%s printer=%s", pj.id, pj.retry_count, printer.name)
except Exception as e:
error_msg = str(e)
logger.error("Print failed for printer %s (%s:%s): %s", printer.name, printer.ip_address, printer.port, e)
logger.error("print.job.failed job=%s attempt=%s printer=%s addr=%s:%s will_retry=yes error=%r",
pj.id, pj.retry_count, printer.name, printer.ip_address, printer.port, error_msg)
log = PrintLog(
order_id=order_id,
@@ -2137,6 +2143,9 @@ def _retry_sweep():
pj.cancelled_at = now
pj.cancel_reason = "order_closed"
db.commit()
# The ticket never printed and never will: this is the job's final failure.
logger.warning("print.job.abandoned job=%s order=%s attempts=%s reason=order_closed",
pj.id, pj.order_id, pj.retry_count)
continue
item_ids = json.loads(pj.item_ids or "[]")
@@ -2151,14 +2160,19 @@ def _retry_sweep():
pj.status = "success"
pj.succeeded_at = now
db.commit()
logger.info("print.job.resolved job=%s reason=items_printed_or_removed", pj.id)
continue
printer = db.query(Printer).filter(Printer.id == pj.printer_id, Printer.is_active == True).first() # noqa: E712
if not printer:
logger.warning("print.job.skipped job=%s printer_id=%s reason=printer_missing_or_inactive",
pj.id, pj.printer_id)
continue
pj.last_attempted_at = now
pj.retry_count = (pj.retry_count or 0) + 1
logger.info("print.job.attempt job=%s attempt=%s printer=%s addr=%s:%s retry=yes",
pj.id, pj.retry_count, printer.name, printer.ip_address, printer.port)
success = False
error_msg = None
@@ -2172,10 +2186,11 @@ def _retry_sweep():
pj.succeeded_at = now
for item in active_unprinted:
item.printed = True
logger.info("PrintJob %s succeeded on retry %d", pj.id, pj.retry_count)
logger.info("print.job.success job=%s attempt=%s printer=%s retry=yes", pj.id, pj.retry_count, printer.name)
except Exception as e:
error_msg = str(e)
logger.warning("PrintJob %s retry %d failed: %s", pj.id, pj.retry_count, e)
logger.error("print.job.failed job=%s attempt=%s printer=%s addr=%s:%s will_retry=yes error=%r",
pj.id, pj.retry_count, printer.name, printer.ip_address, printer.port, error_msg)
db.add(PrintLog(
order_id=pj.order_id,