From d552da1f8d42b7c6db992fee42bc4781f5fdde43 Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Mon, 10 Nov 2014 23:21:31 +0200 Subject: [PATCH] Fixed #22407 -- Added AdminEmailHandler.send_mail(). --- django/utils/log.py | 7 ++++--- docs/releases/1.8.txt | 7 +++++++ docs/topics/logging.txt | 9 ++++++++- tests/logging_tests/tests.py | 21 +++++++++++++++++++-- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/django/utils/log.py b/django/utils/log.py index a2296a3281..6ced3a4a63 100644 --- a/django/utils/log.py +++ b/django/utils/log.py @@ -127,9 +127,10 @@ class AdminEmailHandler(logging.Handler): message = "%s\n\nRequest repr(): %s" % (self.format(record), request_repr) reporter = ExceptionReporter(request, is_email=True, *exc_info) html_message = reporter.get_traceback_html() if self.include_html else None - mail.mail_admins(subject, message, fail_silently=True, - html_message=html_message, - connection=self.connection()) + self.send_mail(subject, message, fail_silently=True, html_message=html_message) + + def send_mail(self, subject, message, *args, **kwargs): + mail.mail_admins(subject, message, *args, connection=self.connection(), **kwargs) def connection(self): return get_connection(backend=self.email_backend, fail_silently=True) diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt index fec2e2b4bc..729c19f3c2 100644 --- a/docs/releases/1.8.txt +++ b/docs/releases/1.8.txt @@ -271,6 +271,13 @@ Internationalization reusable apps. It also allows overriding those custom formats in your main Django project. +Logging +^^^^^^^ + +* The :class:`django.utils.log.AdminEmailHandler` class now has a + :meth:`~django.utils.log.AdminEmailHandler.send_mail` method to make it more + subclass friendly. + Management Commands ^^^^^^^^^^^^^^^^^^^ diff --git a/docs/topics/logging.txt b/docs/topics/logging.txt index e1056e6e96..f44383598f 100644 --- a/docs/topics/logging.txt +++ b/docs/topics/logging.txt @@ -563,8 +563,15 @@ Python logging module. By default, an instance of the email backend specified in :setting:`EMAIL_BACKEND` will be used. -.. _Sentry: https://pypi.python.org/pypi/sentry + .. method:: send_mail(subject, message, *args, **kwargs) + .. versionadded:: 1.8 + + Sends emails to admin users. To customize this behavior, you can + subclass the :class:`~django.utils.log.AdminEmailHandler` class and + override this method. + +.. _Sentry: https://pypi.python.org/pypi/sentry Filters ------- diff --git a/tests/logging_tests/tests.py b/tests/logging_tests/tests.py index 99981e56e1..d0a3a92eff 100644 --- a/tests/logging_tests/tests.py +++ b/tests/logging_tests/tests.py @@ -10,8 +10,9 @@ from django.test import TestCase, RequestFactory, override_settings from django.test.utils import patch_logger from django.utils.encoding import force_text from django.utils.deprecation import RemovedInNextVersionWarning -from django.utils.log import (CallbackFilter, RequireDebugFalse, - RequireDebugTrue) +from django.utils.log import ( + AdminEmailHandler, CallbackFilter, RequireDebugFalse, RequireDebugTrue, +) from django.utils.six import StringIO from admin_scripts.tests import AdminScriptTestCase @@ -341,6 +342,22 @@ class AdminEmailHandlerTest(TestCase): self.assertEqual(msg.subject, "[Django] ERROR (EXTERNAL IP): message") self.assertIn("path:%s" % url_path, msg.body) + @override_settings( + MANAGERS=(('manager', 'manager@example.com'),), + DEBUG=False, + ) + def test_customize_send_mail_method(self): + class ManagerEmailHandler(AdminEmailHandler): + def send_mail(self, subject, message, *args, **kwargs): + mail.mail_managers(subject, message, *args, connection=self.connection(), **kwargs) + + handler = ManagerEmailHandler() + record = self.logger.makeRecord('name', logging.ERROR, 'function', 'lno', 'message', None, None) + self.assertEqual(len(mail.outbox), 0) + handler.emit(record) + self.assertEqual(len(mail.outbox), 1) + self.assertEqual(mail.outbox[0].to, ['manager@example.com']) + class SettingsConfigTest(AdminScriptTestCase): """