# -*- coding: utf-8 -*- """実行ログ出力モジュール。日付別ファイル・UTF-8 BOM付き。""" from __future__ import annotations from datetime import datetime from pathlib import Path from typing import Callable LOG_ENCODING = "utf-8-sig" def log_path_for_today( log_dir: Path, now_func: Callable[[], datetime] = datetime.now ) -> Path: return log_dir / f"{now_func().strftime('%Y-%m-%d')}.log" def append_log( log_dir: Path, result: str, original_filename: str, name: str | None, date: str | None, note: str = "", now_func: Callable[[], datetime] = datetime.now, ) -> None: log_dir.mkdir(parents=True, exist_ok=True) now = now_func() line = ( f"[{now.strftime('%Y-%m-%d %H:%M:%S')}] " f"結果={result} | 元ファイル={original_filename} | " f"氏名={name or ''} | 日付={date or ''} | 備考={note}\n" ) path = log_path_for_today(log_dir, now_func) with open(path, "a", encoding=LOG_ENCODING) as f: f.write(line)