From 052d11d15b1e0441a020dab9946b9984cde961a3 Mon Sep 17 00:00:00 2001 From: TylerCG <117808427+TylerCG@users.noreply.github.com> Date: Mon, 12 May 2025 09:54:50 -0400 Subject: [PATCH] logging --- Dockerfile | 11 +++++++--- app/main.py | 60 +++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 59 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index f23d43f..7ea966d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,10 +2,10 @@ FROM python:3.11-slim # Install ffmpeg and other dependencies -RUN apt-get update && apt-get install -y \ - ffmpeg \ +RUN apt-get update && apt-get install -y ffmpeg \ && rm -rf /var/lib/apt/lists/* \ && pip install yt_dlp + # && install -y cron # Set environment variables ENV PYTHONDONTWRITEBYTECODE=1 @@ -14,7 +14,7 @@ ENV PYTHONUNBUFFERED=1 # Set work directory WORKDIR /app -RUN mkdir /app/data /app/tv /app/youtube /app/asmr /app/nsfw /app/temp +RUN mkdir /app/data /app/tv /app/youtube /app/asmr /app/nsfw /app/temp /app/data/logs # Install dependencies COPY requirements.txt . @@ -23,8 +23,13 @@ RUN pip install --no-cache-dir -r requirements.txt # Copy project files COPY /app . +# Make sure your script is executable +# RUN chmod +x /app/my_script.sh +# RUN chmod +x /start.sh + # Expose port EXPOSE 8000 # Run the app with Uvicorn CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"] +# CMD ["/start.sh"] diff --git a/app/main.py b/app/main.py index 8fcd2a4..4452353 100644 --- a/app/main.py +++ b/app/main.py @@ -1,24 +1,66 @@ -from fastapi import FastAPI, Request, Form +from fastapi import FastAPI, Request, Form, BackgroundTasks from fastapi.responses import HTMLResponse, JSONResponse from fastapi.templating import Jinja2Templates from fastapi.staticfiles import StaticFiles -from fastapi import BackgroundTasks from functools import partial import json, download, asyncio from typing import Optional +import logging, os +from logging.handlers import TimedRotatingFileHandler +# Ensure log directory exists +os.makedirs("/data/logs", exist_ok=True) + +# Setup timed rotating logger +log_path = "/data/logs/syllabus.log" +logger = logging.getLogger("syllabus_logger") +logger.setLevel(logging.DEBUG) + +# Remove any default handlers +logger.handlers = [] + +# Set up TimedRotatingFileHandler +handler = TimedRotatingFileHandler( + filename=log_path, + when="midnight", # Rotate at midnight + interval=1, # Every 1 day + backupCount=30, # Keep last 7 logs + encoding="utf-8", + utc=False # Use UTC for time reference +) + +formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") +handler.setFormatter(formatter) +logger.addHandler(handler) + +# App setup app = FastAPI() - - app.mount("/data", StaticFiles(directory="/data"), name="data") - - templates = Jinja2Templates(directory="templates") -loop = asyncio.get_running_loop() +loop = asyncio.get_event_loop() -# JSON cache +# Optional cache cached_data = None +# Middleware +@app.middleware("http") +async def log_requests(request: Request, call_next): + try: + response = await call_next(request) + except Exception as e: + logger.exception(f"EXCEPTION: {request.method} {request.url} - {str(e)}") + return JSONResponse( + status_code=500, + content={"detail": "Internal Server Error"}, + ) + + logger.info( + f"request_client={request.client.host}:{request.client.port}, " + f"request_method={request.method}, request_url={request.url}, " + f"status_code={response.status_code}" + ) + return response + # api @app.post("/ebook/download", description="Download an ebook via a url.") async def ebookDownload( @@ -89,7 +131,7 @@ async def ydl(background_tasks: BackgroundTasks, url: str = Form(...), location: except Exception as e: return JSONResponse(status_code=500, content={"status": "error", "message": str(e)}) - +#web ui @app.get("/", include_in_schema=False, response_class=HTMLResponse) async def index(request: Request): global cached_data