Skip to content

Commit 040d85e

Browse files
authored
Merge pull request jazzband#613 from tmarice/master
Log traceback if exceptions are ignored
2 parents 66650af + 52fc78d commit 040d85e

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

changelog.d/611.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Print full exception traceback when logging ignored exceptions

django_redis/cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def _decorator(self, *args, **kwargs):
3232
except ConnectionInterrupted as e:
3333
if self._ignore_exceptions:
3434
if self._log_ignored_exceptions:
35-
self.logger.error(str(e))
35+
self.logger.exception("Exception ignored")
3636

3737
return return_value
3838
raise e.__cause__

tests/test_cache_options.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import pytest
55
from django.core.cache import caches
6+
from pytest import LogCaptureFixture
67
from pytest_django.fixtures import SettingsWrapper
78
from redis.exceptions import ConnectionError
89

@@ -22,8 +23,10 @@ def reverse_key(key: str) -> str:
2223
def ignore_exceptions_cache(settings: SettingsWrapper) -> RedisCache:
2324
caches_setting = copy.deepcopy(settings.CACHES)
2425
caches_setting["doesnotexist"]["OPTIONS"]["IGNORE_EXCEPTIONS"] = True
26+
caches_setting["doesnotexist"]["OPTIONS"]["LOG_IGNORED_EXCEPTIONS"] = True
2527
settings.CACHES = caches_setting
2628
settings.DJANGO_REDIS_IGNORE_EXCEPTIONS = True
29+
settings.DJANGO_REDIS_LOG_IGNORED_EXCEPTIONS = True
2730
return cast(RedisCache, caches["doesnotexist"])
2831

2932

@@ -34,12 +37,22 @@ def test_get_django_omit_exceptions_many_returns_default_arg(
3437
assert ignore_exceptions_cache.get_many(["key1", "key2", "key3"]) == {}
3538

3639

37-
def test_get_django_omit_exceptions(ignore_exceptions_cache: RedisCache):
40+
def test_get_django_omit_exceptions(
41+
caplog: LogCaptureFixture, ignore_exceptions_cache: RedisCache
42+
):
3843
assert ignore_exceptions_cache._ignore_exceptions is True
44+
assert ignore_exceptions_cache._log_ignored_exceptions is True
45+
3946
assert ignore_exceptions_cache.get("key") is None
4047
assert ignore_exceptions_cache.get("key", "default") == "default"
4148
assert ignore_exceptions_cache.get("key", default="default") == "default"
4249

50+
assert len(caplog.records) == 3
51+
assert all(
52+
record.levelname == "ERROR" and record.msg == "Exception ignored"
53+
for record in caplog.records
54+
)
55+
4356

4457
def test_get_django_omit_exceptions_priority_1(settings: SettingsWrapper):
4558
caches_setting = copy.deepcopy(settings.CACHES)

0 commit comments

Comments
 (0)