Skip to content
Advertisement

How to hide Celery task id from structlog?

I used Structlog and Celery in my Django application and both work very well, but I would like to prevent task_id from appearing in the log when the worker executes tasks. How can I do that?

The reason is that task_id is a key:value of 36 characters long so it makes log output hard to read.

JavaScript

This is how my structlog setup for Celery:

JavaScript

Thank you

Advertisement

Answer

The format of a log line is controlled by the formatter set on the handler of the logger in question. If you’re seeing the task ID in the log, that implies that you are hitting the celery task logger (celery.utils.log.get_task_logger). Modify that formatter, and you can configure logging to remove the task ID from the log line. Here is a sample yaml dictionary config you can use with logging.dictConfig to control the format of the celery task logger:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement