Sentry django configuration - logger

2019-06-20 19:30发布

I am trying to use simple logging and want to send errors/exceptions to Sentry.

I configured the Sentry as per the document and run the test successfully on my dev(python manage.py raven test)

I added the Logging configuration as in Sentry documentation to a Django settings

When I put this code in my View, then it doesn't work at all

import logging
logger = logging.getLogger(__name__)
logger.error('There was an error, with a stacktrace!', extra={
    'stack': True,
})

Maybe I am missing something

Thanks for the help

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'root': {
        'level': 'WARNING',
        'handlers': ['sentry'],
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s '
                      '%(process)d %(thread)d %(message)s'
        },
    },
    'handlers': {
        'sentry': {
            'level': 'ERROR', # To capture more than ERROR, change to WARNING, INFO, etc.
            'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
            'tags': {'custom-tag': 'x'},
        },
        '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,
        },
    },
}

2条回答
你好瞎i
2楼-- · 2019-06-20 19:33

At first, you shouldn't use __name__ in .getLogger() method, because it load default logging configs. You can use a defined loggers in logging Django setting:

logger = logging.getLogger('raven')

Then needed to add sentry handlers in you Django logging configs:

'raven': {
    'level': 'DEBUG',
    'handlers': ['console', 'sentry'],
    'propagate': False,
},
查看更多
地球回转人心会变
3楼-- · 2019-06-20 19:52

Integration with Django requires special Raven Django app and RAVEN_CONFIG in settings.py:

INSTALLED_APPS = (
    'raven.contrib.django.raven_compat',
)

RAVEN_CONFIG = {
    'dsn': 'https://<key>:<secret>@sentry.io/<project>',
}

Have you setup them?

查看更多
登录 后发表回答