Skip to content

Commit

Permalink
Removed backwards-compatibility shim for #16288.
Browse files Browse the repository at this point in the history
Also unit-tested django.utils.log.RequireDebugTrue for consistency.
  • Loading branch information
aaugustin committed Dec 29, 2012
1 parent 067505a commit fb9f1b9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 100 deletions.
39 changes: 1 addition & 38 deletions django/conf/__init__.py
Expand Up @@ -74,11 +74,8 @@ def _configure_logging(self):

logging_config_func(DEFAULT_LOGGING)

# ... then invoke it with the logging settings
if self.LOGGING:
# Backwards-compatibility shim for #16288 fix
compat_patch_logging_config(self.LOGGING)

# ... then invoke it with the logging settings
logging_config_func(self.LOGGING)

def configure(self, default_settings=global_settings, **options):
Expand Down Expand Up @@ -195,37 +192,3 @@ def __dir__(self):
return list(self.__dict__) + dir(self.default_settings)

settings = LazySettings()



def compat_patch_logging_config(logging_config):
"""
Backwards-compatibility shim for #16288 fix. Takes initial value of
``LOGGING`` setting and patches it in-place (issuing deprecation warning)
if "mail_admins" logging handler is configured but has no filters.
"""
# Shim only if LOGGING["handlers"]["mail_admins"] exists,
# but has no "filters" key
if "filters" not in logging_config.get(
"handlers", {}).get(
"mail_admins", {"filters": []}):

warnings.warn(
"You have no filters defined on the 'mail_admins' logging "
"handler: adding implicit debug-false-only filter. "
"See http://docs.djangoproject.com/en/dev/releases/1.4/"
"#request-exceptions-are-now-always-logged",
DeprecationWarning)

filter_name = "require_debug_false"

filters = logging_config.setdefault("filters", {})
while filter_name in filters:
filter_name = filter_name + "_"

filters[filter_name] = {
"()": "django.utils.log.RequireDebugFalse",
}

logging_config["handlers"]["mail_admins"]["filters"] = [filter_name]
2 changes: 1 addition & 1 deletion django/utils/log.py
Expand Up @@ -152,4 +152,4 @@ def filter(self, record):

class RequireDebugTrue(logging.Filter):
def filter(self, record):
return settings.DEBUG
return settings.DEBUG
71 changes: 10 additions & 61 deletions tests/regressiontests/logging_tests/tests.py
Expand Up @@ -5,12 +5,12 @@
import sys
import warnings

from django.conf import compat_patch_logging_config, LazySettings
from django.conf import LazySettings
from django.core import mail
from django.test import TestCase, RequestFactory
from django.test.utils import override_settings
from django.utils.encoding import force_text
from django.utils.log import CallbackFilter, RequireDebugFalse
from django.utils.log import CallbackFilter, RequireDebugFalse, RequireDebugTrue
from django.utils.six import StringIO
from django.utils.unittest import skipUnless

Expand Down Expand Up @@ -40,46 +40,10 @@
}


class PatchLoggingConfigTest(TestCase):
"""
Tests for backward-compat shim for #16288. These tests should be removed in
Django 1.6 when that shim and DeprecationWarning are removed.
"""
def test_filter_added(self):
"""
Test that debug-false filter is added to mail_admins handler if it has
no filters.
"""
config = copy.deepcopy(OLD_LOGGING)

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
compat_patch_logging_config(config)
self.assertEqual(len(w), 1)

self.assertEqual(
config["handlers"]["mail_admins"]["filters"],
['require_debug_false'])

def test_filter_configuration(self):
"""
Test that the auto-added require_debug_false filter is an instance of
`RequireDebugFalse` filter class.
"""
config = copy.deepcopy(OLD_LOGGING)
with warnings.catch_warnings(record=True):
compat_patch_logging_config(config)

flt = config["filters"]["require_debug_false"]
self.assertEqual(flt["()"], "django.utils.log.RequireDebugFalse")

class LoggingFiltersTest(TestCase):
def test_require_debug_false_filter(self):
"""
Test the RequireDebugFalse filter class.
"""
filter_ = RequireDebugFalse()

Expand All @@ -89,32 +53,17 @@ def test_require_debug_false_filter(self):
with self.settings(DEBUG=False):
self.assertEqual(filter_.filter("record is not used"), True)

def test_no_patch_if_filters_key_exists(self):
"""
Test that the logging configuration is not modified if the mail_admins
handler already has a "filters" key.
"""
config = copy.deepcopy(OLD_LOGGING)
config["handlers"]["mail_admins"]["filters"] = []
new_config = copy.deepcopy(config)
compat_patch_logging_config(new_config)

self.assertEqual(config, new_config)

def test_no_patch_if_no_mail_admins_handler(self):
def test_require_debug_true_filter(self):
"""
Test that the logging configuration is not modified if the mail_admins
handler is not present.
Test the RequireDebugTrue filter class.
"""
config = copy.deepcopy(OLD_LOGGING)
config["handlers"].pop("mail_admins")
new_config = copy.deepcopy(config)
compat_patch_logging_config(new_config)
filter_ = RequireDebugTrue()

self.assertEqual(config, new_config)
with self.settings(DEBUG=True):
self.assertEqual(filter_.filter("record is not used"), True)

with self.settings(DEBUG=False):
self.assertEqual(filter_.filter("record is not used"), False)

class DefaultLoggingTest(TestCase):
def setUp(self):
Expand Down

0 comments on commit fb9f1b9

Please sign in to comment.