Errors happen, especially when you have a distributed application. Let’s look how Celery allows us to log messages into the Celery logger.
This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.
Get the logger
Celery uses the standard Python logger library, what means we can work with the logger as described in my Python Friday post #62. The only special thing to do is to get the logger from Celery instead of creating a new one:
1 2 3 4 |
import time from celery import Celery from celery.utils.log import get_task_logger logger = get_task_logger(__name__) |
If we use the Celery logger our messages automatically get the name of the task and the unique id:
1 2 3 4 |
@app.task def prepare(order_id): time.sleep(5) # simulate work logger.info(f"order #{order_id} prepared") |
If our task runs it will write a message like this one:
[INFO/MainProcess] celery_task.prepare[b3a376ad-0f27-4fe2-8b28-e4f94c013096]: order #22 prepared
That may help you a lot more to map your log entry to the other things that go on in Celery. As a comparison, the print() statement got us this log entry:
[WARNING/MainProcess] order #20 prepared
Next
We now have the most important parts together. Next week we have a look how to use Celery with a Flask application.
1 thought on “Python Friday #125: Logging in Celery”