Initial commit: Daily News App
企業內部新聞彙整與分析系統 - 自動新聞抓取 (Digitimes, 經濟日報, 工商時報) - AI 智慧摘要 (OpenAI/Claude/Ollama) - 群組管理與訂閱通知 - 已清理 Python 快取檔案 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
81
app/core/logging_config.py
Normal file
81
app/core/logging_config.py
Normal file
@@ -0,0 +1,81 @@
|
||||
"""
|
||||
日誌系統設定模組
|
||||
"""
|
||||
import logging
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from app.core.config import settings
|
||||
|
||||
|
||||
class SensitiveFilter(logging.Filter):
|
||||
"""過濾敏感資訊的日誌過濾器"""
|
||||
|
||||
def filter(self, record):
|
||||
"""過濾包含敏感資訊的日誌訊息"""
|
||||
sensitive_keywords = ['password', 'secret', 'key', 'token', 'api_key', 'db_password']
|
||||
msg = str(record.getMessage()).lower()
|
||||
|
||||
for keyword in sensitive_keywords:
|
||||
if keyword in msg:
|
||||
# 只記錄錯誤類型,不記錄詳細內容
|
||||
record.msg = f"[敏感資訊已過濾] {record.name}"
|
||||
record.args = ()
|
||||
break
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def setup_logging():
|
||||
"""設定日誌系統"""
|
||||
# 建立 logs 目錄
|
||||
log_dir = Path("logs")
|
||||
log_dir.mkdir(exist_ok=True)
|
||||
|
||||
# 設定日誌等級
|
||||
log_level = logging.DEBUG if settings.debug else logging.INFO
|
||||
|
||||
# 設定日誌格式
|
||||
log_format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
||||
date_format = '%Y-%m-%d %H:%M:%S'
|
||||
|
||||
# 設定處理器
|
||||
handlers = [
|
||||
logging.StreamHandler(sys.stdout),
|
||||
logging.FileHandler('logs/app.log', encoding='utf-8')
|
||||
]
|
||||
|
||||
# 如果是生產環境,也記錄錯誤到單獨的檔案
|
||||
if settings.app_env == "production":
|
||||
error_handler = logging.FileHandler('logs/error.log', encoding='utf-8')
|
||||
error_handler.setLevel(logging.ERROR)
|
||||
handlers.append(error_handler)
|
||||
|
||||
# 設定基本配置
|
||||
logging.basicConfig(
|
||||
level=log_level,
|
||||
format=log_format,
|
||||
datefmt=date_format,
|
||||
handlers=handlers
|
||||
)
|
||||
|
||||
# 應用敏感資訊過濾器
|
||||
sensitive_filter = SensitiveFilter()
|
||||
for handler in logging.root.handlers:
|
||||
handler.addFilter(sensitive_filter)
|
||||
|
||||
# 設定第三方庫的日誌等級
|
||||
logging.getLogger("httpx").setLevel(logging.WARNING)
|
||||
logging.getLogger("urllib3").setLevel(logging.WARNING)
|
||||
|
||||
return logging.getLogger(__name__)
|
||||
|
||||
|
||||
# 初始化日誌系統
|
||||
logger = setup_logging()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user