Skip to content

Commit 3fb8372

Browse files
committedNov 22, 2012
Merge branch 'ticket_19325' of https://github.com/hannesstruss/django into hannesstruss-ticket_19325
2 parents d80d05f + f9891f2 commit 3fb8372

File tree

5 files changed

+69
-3
lines changed

5 files changed

+69
-3
lines changed
 

‎AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ answer newbie questions, and generally made Django that much better:
589589
Gasper Zejn <zejn@kiberpipa.org>
590590
Jarek Zgoda <jarek.zgoda@gmail.com>
591591
Cheng Zhang
592+
Hannes Struß <x@hannesstruss.de>
592593

593594
A big THANK YOU goes to:
594595

‎django/utils/log.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from django.conf import settings
55
from django.core import mail
6+
from django.core.mail import get_connection
67
from django.views.debug import ExceptionReporter, get_exception_reporter_filter
78

89

@@ -76,9 +77,10 @@ class AdminEmailHandler(logging.Handler):
7677
request data will be provided in the email report.
7778
"""
7879

79-
def __init__(self, include_html=False):
80+
def __init__(self, include_html=False, email_backend=None):
8081
logging.Handler.__init__(self)
8182
self.include_html = include_html
83+
self.email_backend = email_backend
8284

8385
def emit(self, record):
8486
try:
@@ -110,7 +112,12 @@ def emit(self, record):
110112
message = "%s\n\n%s" % (stack_trace, request_repr)
111113
reporter = ExceptionReporter(request, is_email=True, *exc_info)
112114
html_message = self.include_html and reporter.get_traceback_html() or None
113-
mail.mail_admins(subject, message, fail_silently=True, html_message=html_message)
115+
mail.mail_admins(subject, message, fail_silently=True,
116+
html_message=html_message,
117+
connection=self.connection())
118+
119+
def connection(self):
120+
return get_connection(backend=self.email_backend)
114121

115122
def format_subject(self, subject):
116123
"""

‎docs/topics/logging.txt

+18-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ Handlers
440440
Django provides one log handler in addition to those provided by the
441441
Python logging module.
442442

443-
.. class:: AdminEmailHandler([include_html=False])
443+
.. class:: AdminEmailHandler(include_html=False, email_backend=None)
444444

445445
This handler sends an email to the site admins for each log
446446
message it receives.
@@ -476,6 +476,23 @@ Python logging module.
476476
sensitive information to be filtered out of error reports -- learn more on
477477
:ref:`Filtering error reports<filtering-error-reports>`.
478478

479+
.. versionadded:: 1.6
480+
481+
By setting the ``email_backend`` argument of ``AdminEmailHandler``, the
482+
:ref:`email backend <topic-email-backends>` that is being used by the
483+
handler can be overridden, like this::
484+
485+
'handlers': {
486+
'mail_admins': {
487+
'level': 'ERROR',
488+
'class': 'django.utils.log.AdminEmailHandler',
489+
'email_backend': 'django.core.mail.backends.filebased.EmailBackend',
490+
}
491+
},
492+
493+
By default, an instance of the email backend specified in
494+
:setting:`EMAIL_BACKEND` will be used.
495+
479496
.. _Sentry: http://pypi.python.org/pypi/sentry
480497

481498

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import logging
22

33
from django.conf import settings
4+
from django.core.mail.backends.base import BaseEmailBackend
5+
46

57
class MyHandler(logging.Handler):
68
def __init__(self):
79
logging.Handler.__init__(self)
810
self.config = settings.LOGGING
11+
12+
13+
class MyEmailBackend(BaseEmailBackend):
14+
def send_messages(self, email_messages):
15+
pass

‎tests/regressiontests/logging_tests/tests.py

+34
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
from ..admin_scripts.tests import AdminScriptTestCase
1717

18+
from .logconfig import MyEmailBackend
19+
1820
PYVERS = sys.version_info[:2]
1921

2022
# logging config prior to using filter with mail_admins
@@ -305,6 +307,38 @@ def test_truncate_subject(self):
305307
self.assertEqual(len(mail.outbox), 1)
306308
self.assertEqual(mail.outbox[0].subject, expected_subject)
307309

310+
@override_settings(
311+
ADMINS=(('admin', 'admin@example.com'),),
312+
DEBUG=False,
313+
)
314+
def test_uses_custom_email_backend(self):
315+
"""
316+
Refs #19325
317+
"""
318+
message = 'All work and no play makes Jack a dull boy'
319+
admin_email_handler = self.get_admin_email_handler(self.logger)
320+
mail_admins_called = {'called': False}
321+
322+
def my_mail_admins(*args, **kwargs):
323+
connection = kwargs['connection']
324+
self.assertTrue(isinstance(connection, MyEmailBackend))
325+
mail_admins_called['called'] = True
326+
327+
# Monkeypatches
328+
orig_mail_admins = mail.mail_admins
329+
orig_email_backend = admin_email_handler.email_backend
330+
mail.mail_admins = my_mail_admins
331+
admin_email_handler.email_backend = (
332+
'regressiontests.logging_tests.logconfig.MyEmailBackend')
333+
334+
try:
335+
self.logger.error(message)
336+
self.assertTrue(mail_admins_called['called'])
337+
finally:
338+
# Revert Monkeypatches
339+
mail.mail_admins = orig_mail_admins
340+
admin_email_handler.email_backend = orig_email_backend
341+
308342

309343
class SettingsConfigTest(AdminScriptTestCase):
310344
"""

0 commit comments

Comments
 (0)