How to send celery all logs to a custom handler . in my case python-logstash handler
<p>In my Celery application I am getting 2 types of logs on the console i.e celery application logs and task level logs (inside task I am using logger.INFO(str) syntax for logging)</p>

<p>I wanted to send both of them to a custom handler (in my case python-logstash handler )</p>

<p>For django logs I was successfull, by setting handler and logger in settings.py but I am helpless with celery</p>

<pre><code>def initialize_logstash(logger=None,loglevel=logging.DEBUG, **kwargs):
    # logger = logging.getLogger('celery')
    handler = logstash.TCPLogstashHandler('localhost', 5959,tags=['worker'])
    handler.setLevel(loglevel)
    logger.addHandler(handler)
    # logger.setLevel(logging.DEBUG)
    return logger

from celery.signals import after_setup_task_logger
after_setup_task_logger.connect(initialize_logstash)
from celery.signals import after_setup_logger
after_setup_logger.connect(initialize_logstash)
</code></pre>

<p>using both after_setup_task_logger and after_setup_logger signals solved the problem </p>

python logging logstash django-celery celery-task