From 1582960fd41725ad0d413090336d2bc5e6c650e5 Mon Sep 17 00:00:00 2001 From: Kenichiro NOGI Date: Sun, 2 Aug 2026 10:10:14 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=9F=E8=A1=8C=E3=83=AD=E3=82=B0?= =?UTF-8?q?=E5=87=BA=E5=8A=9B=E3=83=A2=E3=82=B8=E3=83=A5=E3=83=BC=E3=83=AB?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 5 --- PythonProj/ScanOCR/スクリプト/logger.py | 36 ++++++++++ .../ScanOCR/スクリプト/tests/test_logger.py | 65 +++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 PythonProj/ScanOCR/スクリプト/logger.py create mode 100644 PythonProj/ScanOCR/スクリプト/tests/test_logger.py diff --git a/PythonProj/ScanOCR/スクリプト/logger.py b/PythonProj/ScanOCR/スクリプト/logger.py new file mode 100644 index 00000000..839a46ea --- /dev/null +++ b/PythonProj/ScanOCR/スクリプト/logger.py @@ -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) diff --git a/PythonProj/ScanOCR/スクリプト/tests/test_logger.py b/PythonProj/ScanOCR/スクリプト/tests/test_logger.py new file mode 100644 index 00000000..eabfb4f1 --- /dev/null +++ b/PythonProj/ScanOCR/スクリプト/tests/test_logger.py @@ -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