How can I configure Django logging
to support different DSNs for different loggers
?
Something like this:
settings.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,
},
}
views.py
import logging
logger = logging.getLogger('sentry2')
logger.error('yeey!')
Try this way it worked for me
1) Write custom handler
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) Change your logging in settings
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) Log your errors anywhere
import logging
logger = logging.getLogger('sentry2')
logger.error('yeey!')
It worked for me, tested with python3.6 django2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With