Ticket #16568: ticket_16568_v2.patch

File ticket_16568_v2.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 637b5f4..29c9812 100644
    a b LOGGING_CONFIG = 'django.utils.log.dictConfig'  
    514514# The default logging configuration. This sends an email to
    515515# the site admins on every HTTP 500 error. All other log
    516516# records are sent to the bit bucket.
     517
    517518LOGGING = {
    518519    'version': 1,
    519520    'disable_existing_loggers': False,
    520521    'filters': {
    521522        'require_debug_false': {
    522             '()': 'django.utils.log.CallbackFilter',
    523             'callback': lambda r: not DEBUG
     523            '()': 'django.utils.log.RequireDebugFalse',
    524524        }
    525525    },
    526526    '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 a8098fc..8ce37f5 100644
    a b logger = getLogger('django')  
    3030if not logger.handlers:
    3131    logger.addHandler(NullHandler())
    3232
     33
    3334class AdminEmailHandler(logging.Handler):
    3435    """An exception log handler that emails log entries to site admins.
    3536
    class CallbackFilter(logging.Filter):  
    8283    def __init__(self, callback):
    8384        self.callback = callback
    8485
    85 
    8686    def filter(self, record):
    8787        if self.callback(record):
    8888            return 1
    8989        return 0
     90
     91
     92class RequireDebugFalse(logging.Filter):
     93    def filter(self, record):
     94        return not settings.DEBUG
  • docs/releases/1.4.txt

    diff --git a/docs/releases/1.4.txt b/docs/releases/1.4.txt
    index 90c5e24..f12db28 100644
    a b to :class:`django.utils.log.AdminEmailHandler` to prevent admin error emails in  
    593593
    594594   'filters': {
    595595        'require_debug_false': {
    596             '()': 'django.utils.log.CallbackFilter',
    597             'callback': lambda r: not DEBUG
     596            '()': 'django.utils.log.RequireDebugFalse'
    598597        }
    599598    },
    600599    'handlers': {
  • docs/topics/logging.txt

    diff --git a/docs/topics/logging.txt b/docs/topics/logging.txt
    index f23b529..3b4e26a 100644
    a b Python logging module.  
    504504Filters
    505505-------
    506506
    507 Django provides one log filter in addition to those provided by the
     507Django provides two log filter in addition to those provided by the
    508508Python logging module.
    509509
    510510.. class:: CallbackFilter(callback)
    Python logging module.  
    516516   through the filter. Handling of that record will not proceed if the callback
    517517   returns False.
    518518
     519.. class:: RequireDebugFalse()
     520
     521   .. versionadded:: 1.4
     522
     523   This filter will only proceed records when settings.DEBUG is False.
     524
    519525   This filter is used as follows in the default :setting:`LOGGING`
    520526   configuration to ensure that the :class:`AdminEmailHandler` only sends error
    521527   emails to admins when :setting:`DEBUG` is `False`::
    522528
    523529       'filters': {
    524530            'require_debug_false': {
    525                 '()': 'django.utils.log.CallbackFilter',
    526                 'callback': lambda r: not DEBUG
     531                '()': 'django.utils.log.RequireDebugFalse',
    527532            }
    528533        },
    529534        'handlers': {
  • tests/regressiontests/logging_tests/tests.py

    diff --git a/tests/regressiontests/logging_tests/tests.py b/tests/regressiontests/logging_tests/tests.py
    index 76c5185..354950c 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
     8from django.utils.log import CallbackFilter, RequireDebugFalse, getLogger
    79from django.test.utils import override_settings
    8 from django.utils.log import CallbackFilter, getLogger
    910from django.core import mail
    1011
    1112
    12 
    1313# logging config prior to using filter with mail_admins
    1414OLD_LOGGING = {
    1515    'version': 1,
    OLD_LOGGING = {  
    3030}
    3131
    3232
    33 
    3433class PatchLoggingConfigTest(TestCase):
    3534    """
    3635    Tests for backward-compat shim for #16288. These tests should be removed in
    class PatchLoggingConfigTest(TestCase):  
    5049            config["handlers"]["mail_admins"]["filters"],
    5150            ['require_debug_false'])
    5251
    53 
    5452    def test_filter_configuration(self):
    5553        """
    56         Test that the debug-false filter is a CallbackFilter with a callback
    57         that works as expected (returns ``not DEBUG``).
     54        Test that the require_debug_false filter (RequireDebugFalse) is added
     55        to the settings when not explicitly specified in the settings.
    5856
    5957        """
    6058        config = copy.deepcopy(OLD_LOGGING)
    6159        compat_patch_logging_config(config)
    6260
    6361        flt = config["filters"]["require_debug_false"]
     62        self.assertEqual(flt["()"], "django.utils.log.RequireDebugFalse")
    6463
    65         self.assertEqual(flt["()"], "django.utils.log.CallbackFilter")
    66 
    67         callback = flt["callback"]
     64    def test_require_debug_false_filter(self):
     65        """Test the RequireDebugFalse filter"""
     66        filter_ = RequireDebugFalse()
    6867
    6968        with self.settings(DEBUG=True):
    70             self.assertEqual(callback("record is not used"), False)
     69            self.assertEqual(filter_.filter("record is not used"), False)
    7170
    7271        with self.settings(DEBUG=False):
    73             self.assertEqual(callback("record is not used"), True)
    74 
     72            self.assertEqual(filter_.filter("record is not used"), True)
    7573
    7674    def test_no_patch_if_filters_key_exists(self):
    7775        """
    class PatchLoggingConfigTest(TestCase):  
    10098        self.assertEqual(config, new_config)
    10199
    102100
     101class TestRequireDebugFalseNoLoggingSetting(TestCase):
     102    """
     103    Test that RequireDebugFalse works as excpected when LOGGING is not
     104    specified in the project settings. See #16568.
     105    """
     106    def test_require_debug_no_logging_setting(self):
     107        filter_ = RequireDebugFalse()
     108
     109        with self.settings(DEBUG=True):
     110            self.assertEqual(filter_.filter("record is not used"), False)
     111
     112        with self.settings(DEBUG=False):
     113            self.assertEqual(filter_.filter("record is not used"), True)
     114
     115
    103116class CallbackFilterTest(TestCase):
    104117    def test_sense(self):
    105118        f_false = CallbackFilter(lambda r: False)
    class CallbackFilterTest(TestCase):  
    110123
    111124    def test_passes_on_record(self):
    112125        collector = []
     126
    113127        def _callback(record):
    114128            collector.append(record)
    115129            return True
Back to Top