feat: enhance logging by redacting sensitive information and update .gitignore for runtime logs
This commit is contained in:
+22
-3
@@ -2,6 +2,25 @@ import json
|
||||
import os
|
||||
from app.utils.logger import logger
|
||||
|
||||
|
||||
def _redact_for_log(value: object) -> object:
|
||||
if not isinstance(value, dict):
|
||||
return value
|
||||
redacted = dict(value)
|
||||
for key in ("token", "password"):
|
||||
if key in redacted and redacted.get(key):
|
||||
redacted[key] = "***"
|
||||
return redacted
|
||||
|
||||
|
||||
def _redact_server_config_dict(state: dict) -> dict:
|
||||
if not isinstance(state, dict):
|
||||
return {}
|
||||
redacted = dict(state)
|
||||
if redacted.get("token"):
|
||||
redacted["token"] = "***"
|
||||
return redacted
|
||||
|
||||
DEFAULT_SYNC_MODE = "merge_local_primary"
|
||||
LOCAL_PLAYLISTS_FOLDER = "playlists"
|
||||
DEFAULT_PATH_MAPPING = {
|
||||
@@ -62,7 +81,7 @@ class ServerConfig:
|
||||
try:
|
||||
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
|
||||
config = json.load(f)
|
||||
logger.debug(f"Loaded server config: {config}")
|
||||
logger.debug(f"Loaded server config: {_redact_for_log(config)}")
|
||||
except FileNotFoundError:
|
||||
# 如果配置文件不存在,使用默认值
|
||||
self.save()
|
||||
@@ -110,7 +129,7 @@ class ServerConfig:
|
||||
self.backup_enabled = config.get("backup_enabled", False)
|
||||
self.backup_retention_count = config.get("backup_retention_count", 5)
|
||||
logger.info(f"Server config loaded.")
|
||||
logger.debug(f"Current server config: {self.__dict__}")
|
||||
logger.debug(f"Current server config: {_redact_server_config_dict(self.__dict__)}")
|
||||
|
||||
def save(self):
|
||||
_ensure_parent_dir(CONFIG_PATH)
|
||||
@@ -137,7 +156,7 @@ class ServerConfig:
|
||||
with open(CONFIG_PATH, "w", encoding="utf-8") as f:
|
||||
json.dump(config, f, indent=4, ensure_ascii=False)
|
||||
logger.info(f"Server config saved.")
|
||||
logger.debug(f"Saved server config: {config}")
|
||||
logger.debug(f"Saved server config: {_redact_for_log(config)}")
|
||||
|
||||
def set_url(self, url: str) -> None:
|
||||
self.url = url
|
||||
|
||||
Reference in New Issue
Block a user