mirror of
https://github.com/a-mayb3/Kanban_clone_backend.git
synced 2026-03-21 18:15:37 +01:00
started implementing logger
This commit is contained in:
parent
59ec938c77
commit
33c5c008aa
1 changed files with 20 additions and 1 deletions
19
main.py
19
main.py
|
|
@ -1,3 +1,4 @@
|
||||||
|
import logging
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
from fastapi import FastAPI, HTTPException
|
from fastapi import FastAPI, HTTPException
|
||||||
|
|
@ -9,9 +10,19 @@ from routers.auth import router as auth_router
|
||||||
from routers.me import router as me_router
|
from routers.me import router as me_router
|
||||||
from database import init_db
|
from database import init_db
|
||||||
|
|
||||||
|
global_logger = logging.getLogger()
|
||||||
|
global_logger.setLevel(logging.INFO)
|
||||||
|
handler = logging.StreamHandler()
|
||||||
|
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||||
|
handler.setFormatter(formatter)
|
||||||
|
global_logger.addHandler(handler)
|
||||||
|
|
||||||
@asynccontextmanager
|
@asynccontextmanager
|
||||||
async def lifespan(app: FastAPI):
|
async def lifespan(app: FastAPI):
|
||||||
|
logger = global_logger
|
||||||
|
|
||||||
# Place for startup and shutdown events if needed in the future
|
# Place for startup and shutdown events if needed in the future
|
||||||
|
logger.info("Initializing database...")
|
||||||
init_db()
|
init_db()
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
|
@ -55,6 +66,10 @@ from fastapi.responses import JSONResponse
|
||||||
@app.exception_handler(HTTPException)
|
@app.exception_handler(HTTPException)
|
||||||
async def http_exception_handler(request, exc):
|
async def http_exception_handler(request, exc):
|
||||||
"""Custom HTTP exception handler"""
|
"""Custom HTTP exception handler"""
|
||||||
|
|
||||||
|
logger = global_logger
|
||||||
|
logger.error(f"HTTP error occurred: {exc.detail}")
|
||||||
|
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
status_code=exc.status_code,
|
status_code=exc.status_code,
|
||||||
content={
|
content={
|
||||||
|
|
@ -70,6 +85,10 @@ async def http_exception_handler(request, exc):
|
||||||
@app.exception_handler(RequestValidationError)
|
@app.exception_handler(RequestValidationError)
|
||||||
async def validation_exception_handler(request, exc):
|
async def validation_exception_handler(request, exc):
|
||||||
"""Handle validation errors"""
|
"""Handle validation errors"""
|
||||||
|
|
||||||
|
logger = global_logger
|
||||||
|
logger.error(f"Validation error: {exc.errors()}")
|
||||||
|
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
status_code=422,
|
status_code=422,
|
||||||
content={
|
content={
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue