diff options
| author | Jon Dufresne <jon.dufresne@gmail.com> | 2018-06-11 18:12:33 -0700 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2018-06-12 11:53:46 -0400 |
| commit | a7cdf7aafc97d17694056292742b472f94dcec13 (patch) | |
| tree | 3600b7e3a24fa50a5f6845cdd32a42cb3165fa07 /django | |
| parent | add57c7e27fa36be8ebec4df4a2cbad81e318070 (diff) | |
[2.1.x] Restored django.test.utils.patch_logger() for backwards compatibility.
Added back after 607970f31cc07c317f2ebb684c8f3ccc36a95b3e.
Backport of 8dcd43ce06b06cd67dab134099135d976ae9884a from master
Diffstat (limited to 'django')
| -rw-r--r-- | django/test/utils.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/django/test/utils.py b/django/test/utils.py index 6187d3e2be..0e04e54964 100644 --- a/django/test/utils.py +++ b/django/test/utils.py @@ -634,6 +634,29 @@ class ignore_warnings(TestContextDecorator): self.catch_warnings.__exit__(*sys.exc_info()) +@contextmanager +def patch_logger(logger_name, log_level, log_kwargs=False): + """ + Context manager that takes a named logger and the logging level + and provides a simple mock-like list of messages received. + + Use unitttest.assertLogs() if you only need Python 3 support. This + private API will be removed after Python 2 EOL in 2020 (#27753). + """ + calls = [] + + def replacement(msg, *args, **kwargs): + call = msg % args + calls.append((call, kwargs) if log_kwargs else call) + logger = logging.getLogger(logger_name) + orig = getattr(logger, log_level) + setattr(logger, log_level, replacement) + try: + yield calls + finally: + setattr(logger, log_level, orig) + + # On OSes that don't provide tzset (Windows), we can't set the timezone # in which the program runs. As a consequence, we must skip tests that # don't enforce a specific timezone (with timezone.override or equivalent), |
