I started using loguru to log errors and info in my FastAPI app. I want to store these logs in an existing MySQL database.
I have created a middleware and managed to log the errors/info and store them in info.log
file. However, I want to store these logs in existing MySQL data instead of a file. I could not find any resource/tutorial explaining how. I believe that I should replace logger.add("info.log",.....
in the code below with something else, but I have no clue what
from loguru import logger
app = FastAPI()
logger.add("info.log",format="Log: [{extra[log_id]}]: {time} | {level} | {message} ", level="INFO", enqueue = True)
@app.middleware("http")
async def log_middleware(request: Request, call_next):
log_id = generate_request_id()
with logger.contextualize(log_id=log_id):
## some code
logger.info(some logs")
return response