fix(log): add support for CONFIG_DIR through environment variables

This commit is contained in:
InfinityPacer
2024-09-11 22:44:35 +08:00
parent 186476ad31
commit 8b336cf3eb
2 changed files with 14 additions and 4 deletions

View File

@@ -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):

View File

@@ -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():