Commit e2e2a81b authored by supundileepa00's avatar supundileepa00

feat: Configured logger

parent b24b2ab2
# Ignore video files
*.mp4
*.avi
*.mov
*.mkv
*.flv
*.wmv
*.mpeg
*.mpg
*.3gp
*.webm
*.vob
*.m4v
*.ts
\ No newline at end of file
2023-05-19 00:32:23,385 - INFO - Received request at root endpoint.
2023-05-19 00:32:23,385 - INFO - Received request at root endpoint.
2023-05-19 00:32:48,522 - ERROR - Received request at root endpoint.
2023-05-19 00:32:48,522 - ERROR - Received request at root endpoint.
from fastapi import APIRouter, File, HTTPException,UploadFile
from core import setup_logger
router = APIRouter()
logger = setup_logger()
@router.post("/upload/video")
async def upload_video(video: UploadFile = File(...)):
try:
# Save the uploaded video to the "uploads" folder
logger.info("Received request at root endpoint.")
file_location = f"files/{video.filename}"
with open(file_location, "wb") as file:
file.write(video.file.read())
return {"filename": video.filename}
except Exception as e:
logger.info(f"Failed to upload file. {e}")
raise HTTPException(
status_code=500,
detail="Failed to upload the video"
)
\ No newline at end of file
import logging
def setup_logger():
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
# Create a file handler for logging to a file
file_handler = logging.FileHandler('app.log')
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
# Create a stream handler for logging to console
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.INFO)
stream_handler.setFormatter(formatter)
# Add the handlers to the logger
logger.addHandler(file_handler)
logger.addHandler(stream_handler)
return logger
from fastapi import FastAPI
from controllers import users_controller
from controllers import translate_controler, users_controller
from fastapi.responses import RedirectResponse
from fastapi.middleware.cors import CORSMiddleware
from core import setup_logger
app = FastAPI()
logger = setup_logger()
app.include_router(users_controller.router)
app.include_router(translate_controler.router)
# Add cores middleware
origins = [
"http://localhost",
"http://localhost:8080",
]
app.add_middleware(CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"])
@app.get('/')
async def root():
url = app.docs_url or '/docs'
return RedirectResponse(url)
\ No newline at end of file
fastapi
uvicorn
python-multipart==0.0.6
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment