Search code examples
pythondjangologgingsentry

Why does this logger not create Sentry events?


I'm using django and sentry-sdk. In my django settings' logger section I have the following handler:

'loggers': {
    'django.request': {
        'level': 'WARNING',                                     
        'handlers': ['console', ],                    
        'propagate': False
    }
}

And the sentry-sdk is initialized as follows:

import logging                                                                    
import sentry_sdk                                                                 
from sentry_sdk.integrations.django import DjangoIntegration                      
from sentry_sdk.integrations.logging import LoggingIntegration

sentry_logging = LoggingIntegration(                                              
    level=logging.INFO,
    event_level=logging.ERROR
)                                                                                 
sentry_sdk.init(                                                                  
    dsn="...",              
    integrations=[DjangoIntegration(), sentry_logging],                           
)                                                                                 

However, the following example does not send an error event to sentry

import logging
logger = logging.getLogger('django.request')
logger.error('Why do I not appear in sentry?')

On the other hand, others do, as for example

import logging
logger = logging.getLogger('another_module')
logger.error('And why do I do appear in sentry?')

Question: How can I fix this for modules with propagate=False?


Solution

  • It's sentry_sdk's django integration that actively sets the logger to be ignored

    ignore_logger("django.server")
    ignore_logger("django.request")
    

    https://github.com/getsentry/sentry-python/blob/master/sentry_sdk/integrations/django/init.py#L90