增加自訂的logger handler - jenhaoyang/backend_blog GitHub Wiki

如果不要取代原本的handler而只是要增加可以用下面範例:

from celery import signals

import logstash
import logging


@signals.after_setup_logger.connect
def setup_logstash_logger(logger, *args, **kwargs):
    handler = logstash.TCPLogstashHandler('localhost', 5959)

    # More logger/handler configuration
    # handler.setLevel(logging.ERROR)
    # ...

    logger.addHandler(handler)

如果要取代原本的handler則

manual configuration of all logging you currently have to listen to the setup_logging signal:

    import celery.signals

    @celery.signals.setup_logging.connect
    def on_celery_setup_logging(**kwargs):
        #取代原本的handler...

參考:
https://stackoverflow.com/a/57973719
https://github.com/celery/celery/issues/3428#issuecomment-245058020