From eaec158f2f29b1e9f7165915c5ffd4e4a01a6ad0 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Mon, 25 May 2026 19:11:44 +0200 Subject: [PATCH] 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 --- collect.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/collect.py b/collect.py index 0295978..6cedc33 100644 --- a/collect.py +++ b/collect.py @@ -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)