Add midnight UTC log rotation for auto-named output files

When -o is not set, the collector detects a UTC date change at the
top of each collection cycle and opens a fresh timestamped file under
logs/.  Explicit -o paths are never rotated.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-25 19:11:44 +02:00
co-authored by Claude Sonnet 4.6
parent e56637f132
commit eaec158f2f
+16 -2
View File
@@ -368,6 +368,11 @@ def save_store(path: Path, store: dict, max_records: int | None) -> None:
# ── main loop ────────────────────────────────────────────────────────────────
def _auto_out_path(logs_dir: Path, vin: str) -> Path:
stamp = datetime.now(timezone.utc).strftime("%Y_%m_%d_%H_%M_%S")
return logs_dir / f"{stamp}_{vin}.json"
def run(args: argparse.Namespace) -> None:
logging.basicConfig(
level=logging.DEBUG if args.verbose else logging.INFO,
@@ -413,12 +418,13 @@ def run(args: argparse.Namespace) -> None:
if args.output:
out = Path(args.output)
logs_dir = None
else:
logs_dir = Path("logs")
logs_dir.mkdir(exist_ok=True)
stamp = datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
out = logs_dir / f"{stamp}_{vin}.json"
out = _auto_out_path(logs_dir, vin)
current_day = datetime.now(timezone.utc).date()
store = load_store(out, vin, args.interval)
log.info(
"Collecting [%s] every %ds → %s (Ctrl-C to stop)",
@@ -429,6 +435,14 @@ def run(args: argparse.Namespace) -> None:
try:
while True:
if logs_dir is not None:
today = datetime.now(timezone.utc).date()
if today != current_day:
out = _auto_out_path(logs_dir, vin)
store = load_store(out, vin, args.interval)
current_day = today
log.info("Midnight UTC rotation → %s", out)
try:
wc.update()
snapshot = collect_snapshot(vehicle, domains)