Ticket #16568: ticket_16568.patch

File ticket_16568.patch, 7.6 KB (added by Andreas Pelme, 13 years ago)
  • django/conf/__init__.py

    diff --git a/django/conf/__init__.py b/django/conf/__init__.py
    index 4337bd4..3b829ce 100644
    a b def compat_patch_logging_config(logging_config):  
    199199        while filter_name in filters:
    200200            filter_name = filter_name + "_"
    201201
    202         def _callback(record):
    203             from django.conf import settings
    204             return not settings.DEBUG
    205 
    206202        filters[filter_name] = {
    207             "()": "django.utils.log.CallbackFilter",
    208             "callback": _callback
    209             }
     203            "()": "django.utils.log.RequireDebugFalse",
     204        }
    210205
    211206        logging_config["handlers"]["mail_admins"]["filters"] = [filter_name]
  • django/conf/global_settings.py

    diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py
    index 5ee12d7..4eb31f5 100644
    a b LOGGING_CONFIG = 'django.utils.log.dictConfig'  
    522522# The default logging configuration. This sends an email to
    523523# the site admins on every HTTP 500 error. All other log
    524524# records are sent to the bit bucket.
     525
    525526LOGGING = {
    526527    'version': 1,
    527528    'disable_existing_loggers': False,
    528529    'filters': {
    529530        'require_debug_false': {
    530             '()': 'django.utils.log.CallbackFilter',
    531             'callback': lambda r: not DEBUG
     531            '()': 'django.utils.log.RequireDebugFalse',
    532532        }
    533533    },
    534534    'handlers': {
  • django/conf/project_template/settings.py

    diff --git a/django/conf/project_template/settings.py b/django/conf/project_template/settings.py
    index 3a2243f..b92c116 100644
    a b LOGGING = {  
    128128    'disable_existing_loggers': False,
    129129    'filters': {
    130130        'require_debug_false': {
    131             '()': 'django.utils.log.CallbackFilter',
    132             'callback': lambda r: not DEBUG
     131            '()': 'django.utils.log.RequireDebugFalse'
    133132        }
    134133    },
    135134    'handlers': {
  • django/utils/log.py

    diff --git a/django/utils/log.py b/django/utils/log.py
    index 1c3fe70..1da7df0 100644
    a b logger = getLogger('django')  
    3030if not logger.handlers:
    3131    logger.addHandler(NullHandler())
    3232
     33
    3334class AdminEmailHandler(logging.Handler):
    3435    def __init__(self, include_html=False):
    3536        logging.Handler.__init__(self)
    class CallbackFilter(logging.Filter):  
    8182    def __init__(self, callback):
    8283        self.callback = callback
    8384
    84 
    8585    def filter(self, record):
    8686        if self.callback(record):
    8787            return 1
    8888        return 0
     89
     90
     91class RequireDebugFalse(logging.Filter):
     92    def filter(self, record):
     93        return not settings.DEBUG
  • docs/releases/1.4.txt

    diff --git a/docs/releases/1.4.txt b/docs/releases/1.4.txt
    index 2723b42..a775dad 100644
    a b to :class:`django.utils.log.AdminEmailHandler` to prevent admin error emails in  
    485485
    486486   'filters': {
    487487        'require_debug_false': {
    488             '()': 'django.utils.log.CallbackFilter',
    489             'callback': lambda r: not DEBUG
     488            '()': 'django.utils.log.RequireDebugFalse'
    490489        }
    491490    },
    492491    'handlers': {
  • docs/topics/logging.txt

    diff --git a/docs/topics/logging.txt b/docs/topics/logging.txt
    index 9137038..472dcff 100644
    a b Python logging module.  
    506506Filters
    507507-------
    508508
    509 Django provides one log filter in addition to those provided by the
     509Django provides two log filter in addition to those provided by the
    510510Python logging module.
    511511
    512512.. class:: CallbackFilter(callback)
    Python logging module.  
    518518   through the filter. Handling of that record will not proceed if the callback
    519519   returns False.
    520520
     521.. class:: RequireDebugFalse()
     522
     523   .. versionadded:: 1.4
     524
     525   This filter will only proceed records when settings.DEBUG is False.
     526
    521527   This filter is used as follows in the default :setting:`LOGGING`
    522528   configuration to ensure that the :class:`AdminEmailHandler` only sends error
    523529   emails to admins when :setting:`DEBUG` is `False`::
    524530
    525531       'filters': {
    526532            'require_debug_false': {
    527                 '()': 'django.utils.log.CallbackFilter',
    528                 'callback': lambda r: not DEBUG
     533                '()': 'django.utils.log.RequireDebugFalse',
    529534            }
    530535        },
    531536        'handlers': {
  • tests/regressiontests/logging_tests/tests.py

    diff --git a/tests/regressiontests/logging_tests/tests.py b/tests/regressiontests/logging_tests/tests.py
    index fe8b169..b34ca78 100644
    a b from __future__ import with_statement  
    22
    33import copy
    44
    5 from django.conf import compat_patch_logging_config
     5from django.conf import compat_patch_logging_config, settings, global_settings
    66from django.test import TestCase
    7 from django.utils.log import CallbackFilter
     7from django.utils.log import CallbackFilter, RequireDebugFalse
    88
    99
    1010# logging config prior to using filter with mail_admins
    OLD_LOGGING = {  
    2727}
    2828
    2929
    30 
    3130class PatchLoggingConfigTest(TestCase):
    3231    """
    3332    Tests for backward-compat shim for #16288. These tests should be removed in
    class PatchLoggingConfigTest(TestCase):  
    4746            config["handlers"]["mail_admins"]["filters"],
    4847            ['require_debug_false'])
    4948
    50 
    5149    def test_filter_configuration(self):
    5250        """
    53         Test that the debug-false filter is a CallbackFilter with a callback
    54         that works as expected (returns ``not DEBUG``).
     51        Test that the require_debug_false filter (RequireDebugFalse) is added
     52        to the settings when not explicitly specified in the settings.
    5553
    5654        """
    5755        config = copy.deepcopy(OLD_LOGGING)
    5856        compat_patch_logging_config(config)
    5957
    6058        flt = config["filters"]["require_debug_false"]
     59        self.assertEqual(flt["()"], "django.utils.log.RequireDebugFalse")
    6160
    62         self.assertEqual(flt["()"], "django.utils.log.CallbackFilter")
    63 
    64         callback = flt["callback"]
     61    def test_require_debug_false_filter(self):
     62        """Test the RequireDebugFalse filter"""
     63        filter_ = RequireDebugFalse()
    6564
    6665        with self.settings(DEBUG=True):
    67             self.assertEqual(callback("record is not used"), False)
     66            self.assertEqual(filter_.filter("record is not used"), False)
    6867
    6968        with self.settings(DEBUG=False):
    70             self.assertEqual(callback("record is not used"), True)
    71 
     69            self.assertEqual(filter_.filter("record is not used"), True)
    7270
    7371    def test_no_patch_if_filters_key_exists(self):
    7472        """
    class PatchLoggingConfigTest(TestCase):  
    9795        self.assertEqual(config, new_config)
    9896
    9997
     98class TestRequireDebugFalseNoLoggingSetting(TestCase):
     99    """
     100    Test that RequireDebugFalse works as excpected when LOGGING is not
     101    specified in the project settings. See #16568.
     102    """
     103    def setUp(self):
     104        self.old_logging = settings.LOGGING
     105        settings.LOGGING = global_settings.LOGGING
     106
     107    def tearDown(self):
     108        settings.LOGGING = self.old_logging
     109
     110    def test_require_debug_no_logging_setting(self):
     111        filter_ = RequireDebugFalse()
     112
     113        with self.settings(DEBUG=True):
     114            self.assertEqual(filter_.filter("record is not used"), False)
     115
     116        with self.settings(DEBUG=False):
     117            self.assertEqual(filter_.filter("record is not used"), True)
     118
     119
    100120class CallbackFilterTest(TestCase):
    101121    def test_sense(self):
    102122        f_false = CallbackFilter(lambda r: False)
    class CallbackFilterTest(TestCase):  
    107127
    108128    def test_passes_on_record(self):
    109129        collector = []
     130
    110131        def _callback(record):
    111132            collector.append(record)
    112133            return True
Back to Top