diff --git a/src/main.py b/src/main.py index b6618bb..ba6263c 100644 --- a/src/main.py +++ b/src/main.py @@ -1,18 +1,26 @@ +import logging import sys from PySide6.QtWidgets import QApplication from app.main_window import MainWindow -from version import APP_NAME +from services.log_service import setup_logging +from version import APP_NAME, APP_VERSION + +logger = logging.getLogger(__name__) def main(): + setup_logging() + logger.info("Application starting: %s %s", APP_NAME, APP_VERSION) + app = QApplication(sys.argv) app.setApplicationName(APP_NAME) window = MainWindow() window.show() + logger.info("Main window displayed") return app.exec() diff --git a/src/services/log_service.py b/src/services/log_service.py index 4bdc680..af4f219 100644 --- a/src/services/log_service.py +++ b/src/services/log_service.py @@ -1,8 +1,45 @@ import logging +import os +from datetime import datetime -def setup_logging(): - logging.basicConfig( - level=logging.INFO, - format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", - ) +def setup_logging(log_dir=None): + """Initialize file + console logging. Call once at application startup. + + log_dir: absolute path to log directory. Defaults to /logs/. + If the directory cannot be created, falls back to console-only logging. + """ + if log_dir is None: + # src/services/log_service.py -> src/services -> src -> project root + _here = os.path.dirname(os.path.abspath(__file__)) + log_dir = os.path.join(os.path.dirname(os.path.dirname(_here)), "logs") + + try: + os.makedirs(log_dir, exist_ok=True) + except OSError as exc: + logging.basicConfig( + level=logging.INFO, + format="%(asctime)s %(name)s %(levelname)s %(message)s", + ) + logging.getLogger(__name__).warning( + "Cannot create log directory %s: %s. Falling back to console only.", log_dir, exc + ) + return + + timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") + log_file = os.path.join(log_dir, "app_{}.log".format(timestamp)) + + formatter = logging.Formatter("%(asctime)s %(name)s %(levelname)s %(message)s") + + root = logging.getLogger() + root.setLevel(logging.INFO) + + fh = logging.FileHandler(log_file, encoding="utf-8") + fh.setFormatter(formatter) + root.addHandler(fh) + + sh = logging.StreamHandler() + sh.setFormatter(formatter) + root.addHandler(sh) + + logging.getLogger(__name__).info("Logging initialized. Log file: %s", log_file) diff --git a/tasks.md b/tasks.md index 07258ff..f25c6bb 100644 --- a/tasks.md +++ b/tasks.md @@ -64,18 +64,18 @@ 任务: -- [ ] 先读取 `src/services/log_service.py` 现有内容 -- [ ] 完善 `src/services/log_service.py` -- [ ] 启动时创建 `logs/` 目录 -- [ ] 日志文件按启动时间或日期命名 -- [ ] 日志格式包含时间、模块、级别、消息 -- [ ] 在 `src/main.py` 中初始化日志 +- [x] 先读取 `src/services/log_service.py` 现有内容 +- [x] 完善 `src/services/log_service.py` +- [x] 启动时创建 `logs/` 目录 +- [x] 日志文件按启动时间或日期命名 +- [x] 日志格式包含时间、模块、级别、消息 +- [x] 在 `src/main.py` 中初始化日志 验收: -- [ ] 启动程序后生成日志文件 -- [ ] 程序启动信息写入日志 -- [ ] 不使用 `print` 作为正式日志 +- [x] 启动程序后生成日志文件 +- [x] 程序启动信息写入日志 +- [x] 不使用 `print` 作为正式日志 ### 1.2 路径与资源服务