Ticket #18993: 18993-2.diff

File 18993-2.diff, 7.6 KB (added by Claude Paroz, 12 years ago)

Second approach based on global_settings.LOGGING

  • django/conf/__init__.py

    diff --git a/django/conf/__init__.py b/django/conf/__init__.py
    index 6272f4e..ad8a1cf 100644
    a b class Settings(BaseSettings):  
    143143            # Backwards-compatibility shim for #16288 fix
    144144            compat_patch_logging_config(self.LOGGING)
    145145
     146            def check_in_dict(key1, key2):
     147                if not key2 in self.LOGGING.get(key1, {}):
     148                    self.LOGGING[key1][key2] = global_settings.LOGGING[key1][key2]
     149            check_in_dict('filters', 'require_debug_true')
     150            check_in_dict('handlers', 'console')
     151            check_in_dict('loggers', 'django')
     152
    146153            # ... then invoke it with the logging settings
    147154            logging_config_func(self.LOGGING)
    148155
  • django/conf/global_settings.py

    diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py
    index 13f7991..2274f9a 100644
    a b LOGGING = {  
    559559    'filters': {
    560560        'require_debug_false': {
    561561            '()': 'django.utils.log.RequireDebugFalse',
    562         }
     562        },
     563        'require_debug_true': {
     564            '()': 'django.utils.log.RequireDebugTrue',
     565        },
    563566    },
    564567    'handlers': {
     568        'console':{
     569            'level': 'INFO',
     570            'class': 'logging.StreamHandler',
     571        },
    565572        'mail_admins': {
    566573            'level': 'ERROR',
    567574            'filters': ['require_debug_false'],
    LOGGING = {  
    569576        }
    570577    },
    571578    'loggers': {
     579        'django': {
     580            'handlers': ['console'],
     581            'filters': ['require_debug_true'],
     582        },
    572583        'django.request': {
    573584            'handlers': ['mail_admins'],
    574585            'level': 'ERROR',
  • django/utils/log.py

    diff --git a/django/utils/log.py b/django/utils/log.py
    index df2089f..2400de2 100644
    a b try:  
    2121except ImportError:
    2222    from django.utils.dictconfig import dictConfig
    2323
     24# This is a remainder of an old 2.4 shim: to be deprecated or simply removed
    2425getLogger = logging.getLogger
    2526
    26 # Ensure the creation of the Django logger
    27 # with a null handler. This ensures we don't get any
    28 # 'No handlers could be found for logger "django"' messages
    29 logger = getLogger('django')
    30 if not logger.handlers:
    31     logger.addHandler(NullHandler())
    32 
    33 
    3427class AdminEmailHandler(logging.Handler):
    3528    """An exception log handler that emails log entries to site admins.
    3629
    class CallbackFilter(logging.Filter):  
    10396class RequireDebugFalse(logging.Filter):
    10497    def filter(self, record):
    10598        return not settings.DEBUG
     99
     100
     101class RequireDebugTrue(logging.Filter):
     102    def filter(self, record):
     103       return settings.DEBUG
  • tests/regressiontests/logging_tests/tests.py

    diff --git a/tests/regressiontests/logging_tests/tests.py b/tests/regressiontests/logging_tests/tests.py
    index f444e0f..dd2fc82 100644
    a b from django.core import mail  
    99from django.test import TestCase, RequestFactory
    1010from django.test.utils import override_settings
    1111from django.utils.log import CallbackFilter, RequireDebugFalse
     12from django.utils.six import StringIO
    1213
    1314
    1415# logging config prior to using filter with mail_admins
    class PatchLoggingConfigTest(TestCase):  
    107108        self.assertEqual(config, new_config)
    108109
    109110
     111class DefaultLoggingTest(TestCase):
     112    def setUp(self):
     113        self.logger = logging.getLogger('django')
     114        self.old_stream = self.logger.handlers[0].stream
     115
     116    def tearDown(self):
     117        self.logger.handlers[0].stream = self.old_stream
     118
     119    def test_django_logger(self):
     120        """
     121        The 'django' base logger only output anything when DEBUG=True.
     122        """
     123        output = StringIO()
     124        self.logger.handlers[0].stream = output
     125        self.logger.error("Hey, this is an error.")
     126        self.assertEqual(output.getvalue(), '')
     127
     128        with self.settings(DEBUG=True):
     129            self.logger.error("Hey, this is an error.")
     130            self.assertEqual(output.getvalue(), 'Hey, this is an error.\n')
     131
     132
    110133class CallbackFilterTest(TestCase):
    111134    def test_sense(self):
    112135        f_false = CallbackFilter(lambda r: False)
    class CallbackFilterTest(TestCase):  
    129152
    130153
    131154class AdminEmailHandlerTest(TestCase):
     155    def setUp(self):
     156        self.logger = logging.getLogger('django.request')
     157        self.old_propagate = self.logger.propagate
     158        self.logger.propagate = False
     159
     160    def tearDown(self):
     161        self.logger.propagate =  self.old_propagate
    132162
    133163    def get_admin_email_handler(self, logger):
    134164        # Inspired from regressiontests/views/views.py: send_log()
    class AdminEmailHandlerTest(TestCase):  
    154184        token1 = 'ping'
    155185        token2 = 'pong'
    156186
    157         logger = logging.getLogger('django.request')
    158         admin_email_handler = self.get_admin_email_handler(logger)
     187        admin_email_handler = self.get_admin_email_handler(self.logger)
    159188        # Backup then override original filters
    160189        orig_filters = admin_email_handler.filters
    161190        try:
    162191            admin_email_handler.filters = []
    163192
    164             logger.error(message, token1, token2)
     193            self.logger.error(message, token1, token2)
    165194
    166195            self.assertEqual(len(mail.outbox), 1)
    167196            self.assertEqual(mail.outbox[0].to, ['admin@example.com'])
    class AdminEmailHandlerTest(TestCase):  
    185214        token1 = 'ping'
    186215        token2 = 'pong'
    187216
    188         logger = logging.getLogger('django.request')
    189         admin_email_handler = self.get_admin_email_handler(logger)
     217        admin_email_handler = self.get_admin_email_handler(self.logger)
    190218        # Backup then override original filters
    191219        orig_filters = admin_email_handler.filters
    192220        try:
    193221            admin_email_handler.filters = []
    194222            rf = RequestFactory()
    195223            request = rf.get('/')
    196             logger.error(message, token1, token2,
     224            self.logger.error(message, token1, token2,
    197225                extra={
    198226                    'status_code': 403,
    199227                    'request': request,
    class AdminEmailHandlerTest(TestCase):  
    223251
    224252        self.assertEqual(len(mail.outbox), 0)
    225253
    226         logger = logging.getLogger('django.request')
    227         logger.error(message)
     254        self.logger.error(message)
    228255
    229256        self.assertEqual(len(mail.outbox), 1)
    230257        self.assertFalse('\n' in mail.outbox[0].subject)
    class AdminEmailHandlerTest(TestCase):  
    248275
    249276        self.assertEqual(len(mail.outbox), 0)
    250277
    251         logger = logging.getLogger('django.request')
    252         logger.error(message)
     278        self.logger.error(message)
    253279
    254280        self.assertEqual(len(mail.outbox), 1)
    255281        self.assertEqual(mail.outbox[0].subject, expected_subject)
  • tests/regressiontests/views/views.py

    diff --git a/tests/regressiontests/views/views.py b/tests/regressiontests/views/views.py
    index 2836d1b..f4e7a5c 100644
    a b def raises_template_does_not_exist(request):  
    119119
    120120def send_log(request, exc_info):
    121121    logger = getLogger('django.request')
     122    logger.propagate = False
    122123    # The default logging config has a logging filter to ensure admin emails are
    123124    # only sent with DEBUG=False, but since someone might choose to remove that
    124125    # filter, we still want to be able to test the behavior of error emails
    class Klass(object):  
    227228            return technical_500_response(request, *exc_info)
    228229
    229230def sensitive_method_view(request):
    230     return Klass().method(request)
    231  No newline at end of file
     231    return Klass().method(request)
Back to Top