From e83a4708b506869e1649c3c35c27238260a02e52 Mon Sep 17 00:00:00 2001 From: Woody Date: Thu, 23 Apr 2026 14:09:48 +0800 Subject: [PATCH] feat(backend): add rotating file logging to backend/app/log/ - Configure RotatingFileHandler in main.py (10MB per file, 5 backups) - Log directory auto-created on startup - Add backend/app/log/ to .gitignore Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus --- .gitignore | 4 ++++ backend/app/main.py | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/.gitignore b/.gitignore index cb90efc..552f6ed 100644 --- a/.gitignore +++ b/.gitignore @@ -45,6 +45,10 @@ Thumbs.db # ChromaDB chroma_db/ +# Backend logs +backend/app/log/ +*.log + # Uploads backend/uploads/* !backend/uploads/.gitkeep diff --git a/backend/app/main.py b/backend/app/main.py index 5b1cd3f..8ddb909 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -1,9 +1,36 @@ +import logging +import os +from logging.handlers import RotatingFileHandler +from pathlib import Path + from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from app.routers import ingest, query from app.core.config import get_settings +# Configure logging before app initialization +LOG_DIR = Path(__file__).parent / "log" +LOG_DIR.mkdir(exist_ok=True) + +LOG_FILE = LOG_DIR / "backend.log" +LOG_FORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s" + +# Root logger configuration +logging.basicConfig( + level=logging.INFO, + format=LOG_FORMAT, + handlers=[ + RotatingFileHandler( + LOG_FILE, + maxBytes=10 * 1024 * 1024, # 10MB per file + backupCount=5, # Keep 5 backup files + encoding="utf-8", + ), + logging.StreamHandler(), # Also keep console output + ], +) + settings = get_settings() app = FastAPI(title="RAG Video Q&A", version="1.0.0")