fix: Address high-severity bandit issues
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import os
|
||||
import logging
|
||||
import os
|
||||
from logging.handlers import RotatingFileHandler
|
||||
from typing import Any, List, Optional
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from typing import List, Any, Optional
|
||||
|
||||
# --- Configuration ---
|
||||
# Use project-local logs directory to avoid permission issues
|
||||
@@ -21,30 +22,29 @@ browser_logger = logging.getLogger("browser_logger")
|
||||
browser_logger.setLevel(logging.INFO)
|
||||
|
||||
# Rotating file handler: 10MB max size, keep last 5 backups
|
||||
handler = RotatingFileHandler(LOG_FILE, maxBytes=10*1024*1024, backupCount=5)
|
||||
formatter = logging.Formatter(
|
||||
'%(asctime)s [%(levelname)s] [BROWSER] %(message)s'
|
||||
)
|
||||
handler = RotatingFileHandler(LOG_FILE, maxBytes=10 * 1024 * 1024, backupCount=5)
|
||||
formatter = logging.Formatter("%(asctime)s [%(levelname)s] [BROWSER] %(message)s")
|
||||
handler.setFormatter(formatter)
|
||||
browser_logger.addHandler(handler)
|
||||
|
||||
# --- API Router ---
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
class LogEntry(BaseModel):
|
||||
level: str
|
||||
timestamp: str
|
||||
message: str
|
||||
data: Optional[List[Any]] = None
|
||||
|
||||
|
||||
class LogBatch(BaseModel):
|
||||
logs: List[LogEntry]
|
||||
|
||||
|
||||
@router.post("/api/logs/browser")
|
||||
async def receive_browser_logs(batch: LogBatch):
|
||||
"""
|
||||
Receives a batch of logs from the browser and writes them to the log file.
|
||||
"""
|
||||
"""Receives a batch of logs from the browser and writes them to the log file."""
|
||||
try:
|
||||
for log in batch.logs:
|
||||
# Map browser levels to python logging levels
|
||||
@@ -52,11 +52,11 @@ async def receive_browser_logs(batch: LogBatch):
|
||||
|
||||
log_message = f"[{log.timestamp}] {log.message}"
|
||||
|
||||
if level == 'error':
|
||||
if level == "error":
|
||||
browser_logger.error(log_message)
|
||||
elif level == 'warn':
|
||||
elif level == "warn":
|
||||
browser_logger.warning(log_message)
|
||||
elif level == 'debug':
|
||||
elif level == "debug":
|
||||
browser_logger.debug(log_message)
|
||||
else:
|
||||
browser_logger.info(log_message)
|
||||
|
||||
Reference in New Issue
Block a user