"""
raven.contrib.celery
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:copyright: (c) 2010 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""

try:
    from celery.task import task
except ImportError:
    from celery.decorators import task
from celery.signals import task_failure
from raven.base import Client


class CeleryMixin(object):
    def send_encoded(self, message):
        "Errors through celery"
        self.send_raw.delay(message)

    @task(routing_key='sentry')
    def send_raw(self, message):
        return super(CeleryMixin, self).send_encoded(message)


class CeleryClient(CeleryMixin, Client):
    pass


def register_signal(client):
    @task_failure.connect(weak=False)
    def process_failure_signal(sender, task_id, exception, args, kwargs,
                               traceback, einfo, **kw):
        client.captureException(
            exc_info=einfo.exc_info,
            extra={
                'task_id': task_id,
                'task': sender,
                'args': args,
                'kwargs': kwargs,
            })

@@ -289,24 +289,43 @@
 nals import 
+after_setup_logger,
 task_failure
@@ -355,16 +355,65 @@
  Client%0A
+from raven.handlers.logging import SentryHandler%0A
 %0A%0Aclass 
@@ -721,16 +721,178 @@
  pass%0A%0A%0A
+class CeleryFilter(object):%0A    def filter(self, record):%0A        if record.funcName in ('_log_error'):%0A            return 0%0A        else:%0A            return 1%0A%0A%0A
 def regi
@@ -1316,8 +1316,473 @@
      %7D)%0A
+%0A    @after_setup_logger.connect(weak=False)%0A    def process_logger_event(sender, logger, loglevel, logfile, format,%0A                             colorize, **kw):%0A        import logging%0A        logger = logging.getLogger()%0A        handler = SentryHandler(client)%0A        if handler.__class__ in map(type, logger.handlers):%0A            return False%0A        handler.setLevel(logging.ERROR)%0A        handler.addFilter(CeleryFilter())%0A        logger.addHandler(handler)%0A

send celery log messages >=ERROR to sentry