1

I'm trying to customize date formatting of my celery app's log results when to write into a file. I couldn't find a way to handle this issue.

from celery import Celery
from celery import signals

app = Celery("myApp")

@signals.celeryd_init.connect
def setup_log_format(sender, conf, **kwargs):
    conf.worker_log_format = """
        [%(asctime)s] %(levelno)s [%(levelname)s] %(name)s: %(message)s
    """.strip().format(sender)
    conf.worker_task_log_format = (
        "[%(asctime)s] %(levelno)s [%(levelname)s] %(name)s: %(message)s"
    ).format(sender)

TaskA.py

from celery.utils.log import get_task_logger
logger = get_task_logger(__name__)

if x:
   logger.info(f"this is {x}")

What I see in the log file is:

[2020-07-13 14:59:30,182] 10 [INFO] This is True

What I want to have is:

[2020-07-13T14:59:30.182606Z] 10 [INFO] This is True

Obviously this feature is not implemented yet on celery side. Can you please tell me if you see any way to solve this issue ?

Thanks in advance for your help.

Ps. I use celery with Django fwk.

rom_pep
  • 87
  • 8
  • There are several ways to achieve what you want. Read the following article: https://www.distributedpython.com/2018/08/28/celery-logging/ – DejanLekic Jul 13 '20 at 13:37
  • Thank you for your answer @DejanLekic. Already checked and tested. It didn't help too much. There are also some [anomalies](https://www.distributedpython.com/2018/08/28/celery-logging/#comment-4373587250) in the examples. – rom_pep Jul 13 '20 at 14:20
  • 1
    https://www.distributedpython.com/2018/11/06/celery-task-logger-format/ – DejanLekic Jul 13 '20 at 14:37
  • Sorry. I already know this site and it doesn't helped. This is why I've posted my question. The problem is there is an incompatibility between django settings for logging and the celery. Thanks anyway for your help ! – rom_pep Jul 20 '20 at 12:17

0 Answers0