feat: 実行ログ出力モジュールを追加
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
905633541f
commit
1582960fd4
36
PythonProj/ScanOCR/スクリプト/logger.py
Normal file
36
PythonProj/ScanOCR/スクリプト/logger.py
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
# -*- 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)
|
||||||
65
PythonProj/ScanOCR/スクリプト/tests/test_logger.py
Normal file
65
PythonProj/ScanOCR/スクリプト/tests/test_logger.py
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from logger import LOG_ENCODING, append_log, log_path_for_today
|
||||||
|
|
||||||
|
|
||||||
|
def fixed_now():
|
||||||
|
return datetime(2026, 8, 2, 10, 30, 15)
|
||||||
|
|
||||||
|
|
||||||
|
def test_log_path_for_today_uses_now_func(tmp_path):
|
||||||
|
result = log_path_for_today(tmp_path, now_func=fixed_now)
|
||||||
|
assert result == tmp_path / "2026-08-02.log"
|
||||||
|
|
||||||
|
|
||||||
|
def test_append_log_creates_dir_and_file(tmp_path):
|
||||||
|
log_dir = tmp_path / "ログ"
|
||||||
|
|
||||||
|
append_log(
|
||||||
|
log_dir,
|
||||||
|
result="成功",
|
||||||
|
original_filename="scan001.pdf",
|
||||||
|
name="山田太郎",
|
||||||
|
date="20260802",
|
||||||
|
note="",
|
||||||
|
now_func=fixed_now,
|
||||||
|
)
|
||||||
|
|
||||||
|
log_path = log_dir / "2026-08-02.log"
|
||||||
|
assert log_path.exists()
|
||||||
|
|
||||||
|
content = log_path.read_text(encoding=LOG_ENCODING)
|
||||||
|
assert "[2026-08-02 10:30:15]" in content
|
||||||
|
assert "結果=成功" in content
|
||||||
|
assert "元ファイル=scan001.pdf" in content
|
||||||
|
assert "氏名=山田太郎" in content
|
||||||
|
assert "日付=20260802" in content
|
||||||
|
|
||||||
|
|
||||||
|
def test_append_log_handles_none_name_and_date(tmp_path):
|
||||||
|
log_dir = tmp_path / "ログ"
|
||||||
|
|
||||||
|
append_log(
|
||||||
|
log_dir,
|
||||||
|
result="失敗",
|
||||||
|
original_filename="scan002.pdf",
|
||||||
|
name=None,
|
||||||
|
date=None,
|
||||||
|
note="抽出不十分",
|
||||||
|
now_func=fixed_now,
|
||||||
|
)
|
||||||
|
|
||||||
|
content = (log_dir / "2026-08-02.log").read_text(encoding=LOG_ENCODING)
|
||||||
|
assert "氏名=" in content
|
||||||
|
assert "日付=" in content
|
||||||
|
assert "備考=抽出不十分" in content
|
||||||
|
|
||||||
|
|
||||||
|
def test_append_log_appends_multiple_lines(tmp_path):
|
||||||
|
log_dir = tmp_path / "ログ"
|
||||||
|
|
||||||
|
append_log(log_dir, "成功", "a.pdf", "氏名A", "20260801", "", now_func=fixed_now)
|
||||||
|
append_log(log_dir, "成功", "b.pdf", "氏名B", "20260802", "", now_func=fixed_now)
|
||||||
|
|
||||||
|
content = (log_dir / "2026-08-02.log").read_text(encoding=LOG_ENCODING)
|
||||||
|
assert content.count("結果=成功") == 2
|
||||||
Loading…
Reference in New Issue
Block a user