Unlike the other similar questions on this topic, I'm asking specifically about the logging aspect as to why running tasks via the asyncio loop are not producing logs. The other questions show how to run asyncio task but not the logging issue I'm facing.
I am running a long-running task via spawning a new thread. Celery / BackgroundTasks turned out to not be suitable for me so I was trying this approach suggested here.
I am trying to log some output to a file via a logger. However, the called long-running function does not seem to be outputting everything. Print does not work either.
I'm curious what I'm doing wrong here as right now I have no way of telling if the function is even being run.
import logging
from fastapi import FastAPI
from time import sleep
class WorkerLogger:
def __init__(self) -> None:
self.logger = logging.getLogger('worker')
handler = logging.FileHandler('worker.logs')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
self.logger.addHandler(handler)
self.logger.setLevel(logging.INFO)
def log(self, message: str) -> None:
self.logger.info(message)
worker_logger = WorkerLogger()
async def long_running_task():
worker_logger.log("❌ this does not get logged")
sleep(5)
# Root route
@app.get("/")
async def root():
loop = asyncio.get_running_loop()
worker_logger.log("✅ this will get logged")
newdata = await loop.run_in_executor(None, lambda: long_running_task())
return {"result": "true"}