1

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"}

6
  • I would suggest having a look at this answer, as well as this answer and this answer Commented Jun 12, 2024 at 4:14
  • Thanks @Chris, I'll update the question as one reproducible instead. I already saw those answers though before posting and I still didn't fully understand why for an asyncio running locally it is not able to log to the same file Commented Jun 12, 2024 at 5:40
  • Please have a look at this answer Commented Jun 12, 2024 at 12:31
  • @Chris Well right now I'm trying to just get the logs to be working, I just added the time.sleep() as an example Commented Jun 12, 2024 at 17:07
  • 1
    Please have a look at this answer and this answer as well. Commented Jun 12, 2024 at 17:31

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.