diff options
| author | SnippyCodes <aaryanparik124@gmail.com> | 2026-02-28 11:06:04 +0530 |
|---|---|---|
| committer | Jacob Walls <jacobtylerwalls@gmail.com> | 2026-03-19 12:36:34 -0400 |
| commit | 2d7f899deb2e3e58438a0703d6c3be4227641239 (patch) | |
| tree | 05f40eb4e7ecf97254ebe9af5e76cf4c930087fa | |
| parent | 1786cd881ff4ad9458d56180ae555d92c14e5af8 (diff) | |
Fixed #36958 -- Reloaded logging config when logging settings are changed in tests.
Thanks JaeHyuck Sa and Jake Howard for the reviews.
| -rw-r--r-- | django/test/signals.py | 8 | ||||
| -rw-r--r-- | tests/logging_tests/tests.py | 42 |
2 files changed, 50 insertions, 0 deletions
diff --git a/django/test/signals.py b/django/test/signals.py index cb78b76114..f594ae434b 100644 --- a/django/test/signals.py +++ b/django/test/signals.py @@ -5,6 +5,7 @@ import warnings from asgiref.local import Local from django.apps import apps +from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.core.signals import setting_changed from django.db import connections, router @@ -13,6 +14,7 @@ from django.dispatch import Signal, receiver from django.utils import timezone from django.utils.formats import FORMAT_SETTINGS, reset_format_cache from django.utils.functional import empty +from django.utils.log import configure_logging template_rendered = Signal() @@ -251,3 +253,9 @@ def user_model_swapped(*, setting, **kwargs): from django.contrib.auth import views views.UserModel = UserModel + + +@receiver(setting_changed) +def update_logging_config(*, setting, **kwargs): + if setting in {"LOGGING", "LOGGING_CONFIG"}: + configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) diff --git a/tests/logging_tests/tests.py b/tests/logging_tests/tests.py index a4de7424f8..9690147e81 100644 --- a/tests/logging_tests/tests.py +++ b/tests/logging_tests/tests.py @@ -573,6 +573,10 @@ class SetupConfigureLogging(SimpleTestCase): Calling django.setup() initializes the logging configuration. """ + def tearDown(self): + super().tearDown() + dictConfig.called = False + def test_configure_initializes_logging(self): from django import setup @@ -586,6 +590,44 @@ class SetupConfigureLogging(SimpleTestCase): setup() self.assertTrue(dictConfig.called) + def test_logging_settings_changed(self): + """ + Logging is reconfigured when LOGGING or LOGGING_CONFIG changes. + """ + new_logging_info = { + "version": 1, + "disable_existing_loggers": False, + "loggers": { + "django.test_custom_logger": { + "level": "INFO", + } + }, + } + new_logging_warning = { + "version": 1, + "disable_existing_loggers": False, + "loggers": { + "django.test_custom_logger": { + "level": "WARNING", + } + }, + } + logger = logging.getLogger("django.test_custom_logger") + + with override_settings(LOGGING=new_logging_info): + self.assertEqual(logger.level, logging.INFO) + + # Repeating the operation works. + with override_settings(LOGGING=new_logging_warning): + self.assertEqual(logger.level, logging.WARNING) + + # The default unconfigured level is NOTSET. + self.assertEqual(logger.level, logging.NOTSET) + + self.assertIs(dictConfig.called, False) + with override_settings(LOGGING_CONFIG="logging_tests.tests.dictConfig"): + self.assertIs(dictConfig.called, True) + @override_settings(DEBUG=True, ROOT_URLCONF="logging_tests.urls") class SecurityLoggerTest(LoggingAssertionMixin, SimpleTestCase): |
