Currently I initiate (from views.py) a long running async_task (in services.py). After it kicks off I redirect to another web template (via views.py) where I want to print logs to the template. The other web template calls a function in utils.py to check on the log queue. Here's a sample froms settings.py where I set up the logging stuff:
# Logging
NBM_QUEUE = Queue()
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'memory': {
'class': 'logging.handlers.MemoryHandler',
'capacity': 100,
'level': 'INFO',
},
'console': {
'class': 'logging.StreamHandler',
},
'queue': {
'class': 'logging.handlers.QueueHandler',
'queue': NBM_QUEUE,
},
},
'loggers': {
'nbmApp': {
'handlers': ['queue','memory','console'],
'level': 'INFO',
},
'django': {
'handlers': ['console'],
'level': 'INFO',
},
},
}
And here's the wsgi.py for the project where I start a QueueListener (note I tried this same code in views.py as well with the same result.
import logging
from logging.handlers import QueueListener
from django.conf import settings
logger = logging.getLogger('nbmApp')
queue_listener = QueueListener(settings.NBM_QUEUE, *logger.handlers)
queue_listener.start()
The problem that I am having is that if I write to the logger outside of the async_task I see it populate the queue and I can pass it to the web template. But everything logged within the async_task doesn't make it out of the thread. I can verify that it is logging to the queue while in the thread, but what gets retrieved in utils.py is empty.
Here's utils.py
@csrf_protect
def get_task_status(request):
logger = logging.getLogger('nbmApp')
logger.info("In get task status")
print(list(settings.NBM_QUEUE.queue))
try:
record = settings.NBM_QUEUE.get(block=False)
except Empty:
context = {'logs': ''}
else:
logs = record.getMessage()
context = {'logs': logs}
print("NEW CONTEXT IS")
print(context)
return JsonResponse(context)
So for example the "in get task status" message gets logged to the web template, but nothing from services.py makes it back to utils.py. So somehow it's writing to different queues or loggers.
The method i follow for logging is the same in all of the python scripts and threads
logger = logging.getLogger('nbmApp')
logger.info("message")
Why isn't the logging from the async thread making it into the same queue as the other logger messages, especially when that explicit logger and queue are defined at a high level?