我在Heroku应用程序中使用Django-RQ来处理后台任务.
当我的后台任务发生错误时,它不会被发送到Sentry.
settings.py中的日志记录设置如下:
LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'root': { 'level': 'WARNING', 'handlers': ['sentry'], }, 'filters': { 'require_debug_false': { '()': 'django.utils.log.RequireDebugFalse' } }, 'formatters': { 'verbose': { 'format': '%(levelname)s %(asctime)s %(module)s ' '%(process)d %(thread)d %(message)s' }, "rq_console": { "format": "%(asctime)s %(message)s", "datefmt": "%H:%M:%S", }, }, 'handlers': { 'mail_admins': { 'level': 'ERROR', 'filters': ['require_debug_false'], 'class': 'django.utils.log.AdminEmailHandler' }, "rq_console": { "level": "DEBUG", "class": "rq.utils.ColorizingStreamHandler", "formatter": "rq_console", "exclude": ["%(asctime)s"], }, 'sentry': { 'level': 'ERROR', 'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler' }, 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'verbose' } }, 'loggers': { 'django.db.backends': { 'level': 'ERROR', 'handlers': ['console'], 'propagate': False, }, 'raven': { 'level': 'DEBUG', 'handlers': ['console'], 'propagate': False, }, 'sentry.errors': { 'level': 'DEBUG', 'handlers': ['console'], 'propagate': False, }, "rq.worker": { "handlers": ["rq_console", "sentry"], "level": "DEBUG" }, 'django.request': { 'handlers': ['mail_admins'], 'level': 'ERROR', 'propagate': True, }, }, }
欣赏有关我做错的任何提示.
您将需要开发一个自定义工作器并使用python-rq注册哨兵处理程序.
我假设你正在使用rq.Worker
工人类(django-rq上的默认值).
import rq from raven import Client from raven.transport import HTTPTransport from rq.contrib.sentry import register_sentry class SentryAwareWorker(rq.Worker): def __init__(self, *args, **kwargs): super(SentryAwareWorker, self).__init__(*args, **kwargs) dsn = settings.RAVEN_CONFIG['dsn'] client = Client(dsn, transport=HTTPTransport) register_sentry(client, self)
我提供了一个选项来获得哨兵dsn
和初始化哨兵客户端,但你可以随心所欲地做到这一点.重要的是register_sentry(client, self)
.
然后运行您的工作人员:
python manage.py rqworker queue_1 queue_2 queue_n --worker-class path.to.SentryAwareWorker