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:
+16
-2
@@ -368,6 +368,11 @@ def save_store(path: Path, store: dict, max_records: int | None) -> None:
|
|||||||
|
|
||||||
# ── main loop ────────────────────────────────────────────────────────────────
|
# ── 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:
|
def run(args: argparse.Namespace) -> None:
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
level=logging.DEBUG if args.verbose else logging.INFO,
|
level=logging.DEBUG if args.verbose else logging.INFO,
|
||||||
@@ -413,12 +418,13 @@ def run(args: argparse.Namespace) -> None:
|
|||||||
|
|
||||||
if args.output:
|
if args.output:
|
||||||
out = Path(args.output)
|
out = Path(args.output)
|
||||||
|
logs_dir = None
|
||||||
else:
|
else:
|
||||||
logs_dir = Path("logs")
|
logs_dir = Path("logs")
|
||||||
logs_dir.mkdir(exist_ok=True)
|
logs_dir.mkdir(exist_ok=True)
|
||||||
stamp = datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
|
out = _auto_out_path(logs_dir, vin)
|
||||||
out = logs_dir / f"{stamp}_{vin}.json"
|
|
||||||
|
|
||||||
|
current_day = datetime.now(timezone.utc).date()
|
||||||
store = load_store(out, vin, args.interval)
|
store = load_store(out, vin, args.interval)
|
||||||
log.info(
|
log.info(
|
||||||
"Collecting [%s] every %ds → %s (Ctrl-C to stop)",
|
"Collecting [%s] every %ds → %s (Ctrl-C to stop)",
|
||||||
@@ -429,6 +435,14 @@ def run(args: argparse.Namespace) -> None:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
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:
|
try:
|
||||||
wc.update()
|
wc.update()
|
||||||
snapshot = collect_snapshot(vehicle, domains)
|
snapshot = collect_snapshot(vehicle, domains)
|
||||||
|
|||||||
Reference in New Issue
Block a user