Django / Raven / Sentry:不同DSN的不同记录器

时间:2015-09-11 00:14:41

标签: python django logging sentry raven

如何配置Django <script> var s1_episodes = JSON.parse(<?= $r['s1_episodes']; ?>); var s2_episodes = JSON.parse(<?= $r['s2_episodes']; ?>); var s3_episodes = JSON.parse(<?= $r['s3_episodes']; ?>); </script> 以支持不同logging的不同DSN?

这样的事情:

settings.py

loggers

views.py

  LOGGING = {
    ..
    'handlers': {
        'sentry1': {
            'level': 'ERROR',
            'class': 'raven.contrib.django.handlers.SentryHandler',
            'dsn': '<DSN1>',
        },        
        'sentry2': {
            'level': 'ERROR',
            'class': 'raven.contrib.django.handlers.SentryHandler',
            'dsn': '<DSN2>',
        },
    },
    'loggers': {
        'sentry1':{
            'handlers': ['console', 'sentry1'],
            'level': 'ERROR',
            'propagate': False,
        },
        'sentry2':{
            'handlers': ['console', 'sentry2'],
            'level': 'ERROR',
            'propagate': False,
        },
}

1 个答案:

答案 0 :(得分:3)

尝试这种方式对我有用

1)编写自定义处理程序

from __future__ import absolute_import

import logging

from raven.handlers.logging import SentryHandler


class CustomHandler(SentryHandler):
    def __init__(self, *args, **kwargs):
        dsn = kwargs.pop('dsn', None)

        self.tags = kwargs.pop('tags', None)

        logging.Handler.__init__(self, level=kwargs.get('level', logging.NOTSET))

        super(CustomHandler, self).__init__(dsn, **kwargs)

2)更改您的登录设置

LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
    'verbose': {
        'format': '%(levelname)s %(asctime)s %(module)s '
                  '%(process)d %(thread)d %(message)s'
    },
},
'handlers': {
    'sentry1': {
        'level': 'ERROR',
        'class': 'project.sentry.CustomHandler',
        'dsn': '<DSN1>',
    },
    'sentry2': {
        'level': 'ERROR',
        'class': 'project.sentry.CustomHandler',
        'dsn': '<DSN2>',
    },
    'console': {
        'level': 'DEBUG',
        'class': 'logging.StreamHandler',
        'formatter': 'verbose'
    }
},
'loggers': {
    'sentry1': {
        'handlers': ['sentry1'],
        'level': 'ERROR',
        'propagate': False,
    },
    'sentry2': {
        'handlers': ['sentry2'],
        'level': 'ERROR',
        'propagate': False,
    },
}
}

3)在任何地方记录您的错误

import logging
logger = logging.getLogger('sentry2')
logger.error('yeey!')

它适用于我,使用python3.6 django2测试。