diff --git a/app/log.py b/app/log.py index 008a1eda..b93c795b 100644 --- a/app/log.py +++ b/app/log.py @@ -2,7 +2,7 @@ import inspect import logging from logging.handlers import RotatingFileHandler from pathlib import Path -from typing import Dict, Any +from typing import Dict, Any, Optional import click from pydantic import BaseSettings @@ -14,6 +14,8 @@ class LogSettings(BaseSettings): """ 日志设置 """ + # 配置文件目录 + CONFIG_DIR: Optional[str] = None # 是否为调试模式 DEBUG: bool = False # 日志级别(DEBUG、INFO、WARNING、ERROR等) @@ -27,12 +29,16 @@ class LogSettings(BaseSettings): # 文件日志格式 LOG_FILE_FORMAT: str = "【%(levelname)s】%(asctime)s - %(message)s" + @property + def CONFIG_PATH(self): + return SystemUtils.get_config_path(self.CONFIG_DIR) + @property def LOG_PATH(self): """ 获取日志存储路径 """ - return SystemUtils.get_config_path() / "logs" + return self.CONFIG_PATH / "logs" @property def LOG_MAX_FILE_SIZE_BYTES(self): diff --git a/app/utils/system.py b/app/utils/system.py index ae6593f9..faeb173d 100644 --- a/app/utils/system.py +++ b/app/utils/system.py @@ -5,7 +5,7 @@ import re import shutil import sys from pathlib import Path -from typing import List, Union, Tuple +from typing import List, Union, Tuple, Optional import docker import psutil @@ -473,10 +473,14 @@ class SystemUtils: return os.stat(src).st_dev == os.stat(dest).st_dev @staticmethod - def get_config_path() -> Path: + def get_config_path(config_dir: Optional[str] = None) -> Path: """ 获取配置路径 """ + if not config_dir: + config_dir = os.getenv("CONFIG_DIR") + if config_dir: + return Path(config_dir) if SystemUtils.is_docker(): return Path("/config") elif SystemUtils.is_frozen():