How to replace celery banner with one log line

If you, like me, suffer from celery banner in your logs, but still want to see useful information from it.

Add --quiet option to celery worker command and connect this logging function to celeryd_after_setup signal, it’s practically a copy of the code from the banner.

from celery import VERSION_BANNER
from celery.apps.worker import Worker
from celery.signals import celeryd_after_setup
from celery.five import string_t


@celeryd_after_setup.connect
def log_celery_setup(sender: str, instance: Worker, **kwargs) -> None:
    app = instance.app
    concurrency = str(instance.concurrency)
    if instance.autoscale:
        max, min = instance.autoscale
        concurrency = "{{min={0}, max={1}}}".format(min, max)
    pool = instance.pool_cls
    if not isinstance(pool, string_t):
        pool = pool.__module__
    concurrency += " ({0})".format(pool.split(".")[-1])

    log_items = ", ".join(
        f"{k}={v}"
        for k, v in {
            "hostname": instance.hostname,
            "version": VERSION_BANNER,
            "app": "{0}:{1:#x}".format(app.main or "__main__", id(app)),
            "transport": app.connection().as_uri(),
            "results": app.backend.as_uri(),
            "concurrency": concurrency,
            "queues": "|".join(str(queue) for queue in app.amqp.queues.keys()),
        }.items()
    )

    logger.info(f"action=celery_setup, status=success, {log_items}")

Then you’ll have a message like this:

action=celery_setup, status=success, hostname=celery@Andrey-MBP, version=4.4.7 (cliffs), app=events:0x108974390, transport=redis://localhost:6379/0, results=disabled://, concurrency=12 (prefork), queues=celery

PS: --quiet option does not work in 4.4.7 version https://github.com/celery/celery/issues/6517

comments powered by Disqus