Refactor logging configuration to use AppData directory

- Updated config.example.json to set default log_file to null.
- Modified config.py to resolve log file paths relative to the AppData directory.
- Added methods to get default log directory and log file path in AppData.
- Ensured logging behavior is consistent whether a log_file is specified or not.
This commit is contained in:
claudi 2026-02-20 07:45:21 +01:00
parent b3fd61aed2
commit a8aa54fa5e
8 changed files with 2932 additions and 2884 deletions

View file

@ -126,8 +126,15 @@ class Config:
# Get log file path
log_file = None
if data.get("enable_logging", True):
log_file_str = data.get("log_file", "logs/webdrop_bridge.log")
log_file = Path(log_file_str).resolve()
log_file_str = data.get("log_file", None)
if log_file_str:
log_file = Path(log_file_str)
# If relative path, resolve relative to app data directory instead of cwd
if not log_file.is_absolute():
log_file = Config.get_default_log_dir() / log_file
else:
# Use default log path in app data
log_file = Config.get_default_log_path()
app_name = data.get("app_name", "WebDrop Bridge")
window_title = data.get("window_title", f"{app_name} v{__version__}")
@ -178,7 +185,7 @@ class Config:
else:
app_version = os.getenv("APP_VERSION")
log_level = os.getenv("LOG_LEVEL", "INFO").upper()
log_file_str = os.getenv("LOG_FILE", "logs/webdrop_bridge.log")
log_file_str = os.getenv("LOG_FILE", None)
allowed_roots_str = os.getenv("ALLOWED_ROOTS", "Z:/,C:/Users/Public")
allowed_urls_str = os.getenv("ALLOWED_URLS", "")
webapp_url = os.getenv("WEBAPP_URL", "https://dev.agravity.io/")
@ -227,7 +234,14 @@ class Config:
# Create log file path if logging enabled
log_file = None
if enable_logging:
log_file = Path(log_file_str).resolve()
if log_file_str:
log_file = Path(log_file_str)
# If relative path, resolve relative to app data directory instead of cwd
if not log_file.is_absolute():
log_file = Config.get_default_log_dir() / log_file
else:
# Use default log path in app data
log_file = Config.get_default_log_path()
# Validate webapp URL is not empty
if not webapp_url:
@ -320,7 +334,7 @@ class Config:
"""Get the default configuration file path.
Returns:
Path to default config file
Path to default config file in user's AppData/Roaming
"""
import platform
if platform.system() == "Windows":
@ -329,6 +343,32 @@ class Config:
base = Path.home() / ".config"
return base / "webdrop_bridge" / "config.json"
@staticmethod
def get_default_log_dir() -> Path:
"""Get the default directory for log files.
Always uses user's AppData directory to ensure permissions work
correctly in both development and installed scenarios.
Returns:
Path to default logs directory in user's AppData/Roaming
"""
import platform
if platform.system() == "Windows":
base = Path.home() / "AppData" / "Roaming"
else:
base = Path.home() / ".local" / "share"
return base / "webdrop_bridge" / "logs"
@staticmethod
def get_default_log_path() -> Path:
"""Get the default log file path.
Returns:
Path to default log file in user's AppData/Roaming/webdrop_bridge/logs
"""
return Config.get_default_log_dir() / "webdrop_bridge.log"
def __repr__(self) -> str:
"""Return developer-friendly representation."""
return (