Refactor logging setup and enhance debug information in FastAPI application
This commit is contained in:
4
.vscode/launch.json
vendored
4
.vscode/launch.json
vendored
@ -3,12 +3,14 @@
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Python: FastAPI",
|
||||
"type": "python",
|
||||
"type": "debugpy",
|
||||
"request": "launch",
|
||||
"module": "uvicorn",
|
||||
"args": [
|
||||
"discord_embed.main:app",
|
||||
"--reload",
|
||||
"--log-level",
|
||||
"debug"
|
||||
],
|
||||
"jinja": true,
|
||||
"justMyCode": true
|
||||
|
@ -1,11 +1,14 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from urllib.parse import urljoin
|
||||
|
||||
from discord_embed import settings
|
||||
|
||||
logger: logging.Logger = logging.getLogger("uvicorn.error")
|
||||
|
||||
|
||||
def generate_html_for_videos(
|
||||
url: str,
|
||||
@ -54,4 +57,14 @@ def generate_html_for_videos(
|
||||
with Path.open(file_path, "w", encoding="utf-8") as f:
|
||||
f.write(video_html)
|
||||
|
||||
logger.info("Generated HTML file: %s", html_url)
|
||||
logger.info("Saved HTML file to disk: %s", file_path)
|
||||
logger.info("Screenshot URL: %s", screenshot)
|
||||
logger.info("Video URL: %s", url)
|
||||
logger.info("Video resolution: %dx%d", width, height)
|
||||
logger.info("Filename: %s", filename)
|
||||
logger.info("Domain: %s", domain)
|
||||
logger.info("HTML URL: %s", html_url)
|
||||
logger.info("Time now: %s", time_now_str)
|
||||
|
||||
return html_url
|
||||
|
@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Annotated
|
||||
from urllib.parse import urljoin
|
||||
@ -7,7 +8,7 @@ from urllib.parse import urljoin
|
||||
from fastapi import FastAPI, File, Request, UploadFile
|
||||
from fastapi.responses import HTMLResponse, JSONResponse
|
||||
|
||||
from discord_embed import settings
|
||||
from discord_embed.settings import serve_domain, upload_folder, webhook_url
|
||||
from discord_embed.video_file_upload import do_things
|
||||
from discord_embed.webhook import send_webhook
|
||||
|
||||
@ -18,6 +19,12 @@ app: FastAPI = FastAPI(
|
||||
"url": "https://github.com/TheLovinator1/discord-embed",
|
||||
},
|
||||
)
|
||||
logger: logging.Logger = logging.getLogger("uvicorn.error")
|
||||
|
||||
logger.info("Server started on http://localhost:8000/")
|
||||
logger.debug("\tServe domain: %s", serve_domain)
|
||||
logger.debug("\tUpload folder: %s", upload_folder)
|
||||
logger.debug("\tWebhook URL: %s", webhook_url)
|
||||
|
||||
|
||||
@app.post("/uploadfiles/", description="Where to send a POST request to upload files.")
|
||||
@ -46,10 +53,10 @@ async def upload_file(file: Annotated[UploadFile, File()]) -> JSONResponse:
|
||||
else:
|
||||
filename: str = remove_illegal_chars(file.filename)
|
||||
|
||||
with Path.open(Path(settings.upload_folder, filename), "wb+") as f:
|
||||
with Path.open(Path(upload_folder, filename), "wb+") as f:
|
||||
f.write(file.file.read())
|
||||
|
||||
html_url: str = urljoin(settings.serve_domain, filename)
|
||||
html_url: str = urljoin(serve_domain, filename)
|
||||
|
||||
send_webhook(f"{html_url} was uploaded.")
|
||||
return JSONResponse(content={"html_url": html_url})
|
||||
@ -90,6 +97,7 @@ def remove_illegal_chars(file_name: str) -> str:
|
||||
]
|
||||
for character in illegal_characters:
|
||||
filename: str = filename.replace(character, "")
|
||||
logger.info("Removed illegal character: %s from filename", character)
|
||||
|
||||
return filename
|
||||
|
||||
|
@ -3,10 +3,10 @@ from __future__ import annotations
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from dotenv import find_dotenv, load_dotenv
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# Load environment variables
|
||||
load_dotenv(find_dotenv(), verbose=True)
|
||||
load_dotenv(verbose=True)
|
||||
|
||||
webhook_url: str = os.environ["WEBHOOK_URL"]
|
||||
serve_domain: str = os.environ["SERVE_DOMAIN"].removesuffix("/")
|
||||
|
@ -8,7 +8,7 @@ import ffmpeg
|
||||
|
||||
from discord_embed import settings
|
||||
|
||||
logger: logging.Logger = logging.getLogger(__name__)
|
||||
logger: logging.Logger = logging.getLogger("uvicorn.error")
|
||||
|
||||
|
||||
@dataclass
|
||||
|
@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING
|
||||
@ -11,6 +12,8 @@ from discord_embed.video import Resolution, make_thumbnail, video_resolution
|
||||
if TYPE_CHECKING:
|
||||
from fastapi import UploadFile
|
||||
|
||||
logger: logging.Logger = logging.getLogger("uvicorn.error")
|
||||
|
||||
|
||||
@dataclass
|
||||
class VideoFile:
|
||||
@ -78,4 +81,5 @@ def do_things(file: UploadFile) -> str:
|
||||
screenshot=screenshot_url,
|
||||
filename=video_file.filename,
|
||||
)
|
||||
logger.info("Generated HTML URL: %s", html_url)
|
||||
return html_url
|
||||
|
@ -10,7 +10,7 @@ from discord_embed import settings
|
||||
if TYPE_CHECKING:
|
||||
from requests import Response
|
||||
|
||||
logger: logging.Logger = logging.getLogger(__name__)
|
||||
logger: logging.Logger = logging.getLogger("uvicorn.error")
|
||||
|
||||
|
||||
def send_webhook(message: str) -> None:
|
||||
@ -19,6 +19,7 @@ def send_webhook(message: str) -> None:
|
||||
Args:
|
||||
message: The message to send.
|
||||
"""
|
||||
logger.info("Sending webhook with message: %s", message)
|
||||
webhook: DiscordWebhook = DiscordWebhook(
|
||||
url=settings.webhook_url,
|
||||
content=message or "discord-nice-embed: No message was provided.",
|
||||
|
Reference in New Issue
Block a user