Author: julien
Date: 2011-09-01 20:04:02 -0700 (Thu, 01 Sep 2011)
New Revision: 16715

Modified:
   django/trunk/django/utils/log.py
   django/trunk/tests/regressiontests/logging_tests/tests.py
Log:
Fixed #16736 -- Enabled the merging of user-supplied arguments to format the 
error emails' subject in `AdminEmailHandler`.

Modified: django/trunk/django/utils/log.py
===================================================================
--- django/trunk/django/utils/log.py    2011-08-31 08:07:07 UTC (rev 16714)
+++ django/trunk/django/utils/log.py    2011-09-02 03:04:02 UTC (rev 16715)
@@ -31,15 +31,16 @@
     logger.addHandler(NullHandler())
 
 class AdminEmailHandler(logging.Handler):
-    def __init__(self, include_html=False):
-        logging.Handler.__init__(self)
-        self.include_html = include_html
-
     """An exception log handler that emails log entries to site admins.
 
     If the request is passed as the first argument to the log record,
     request data will be provided in the email report.
     """
+
+    def __init__(self, include_html=False):
+        logging.Handler.__init__(self)
+        self.include_html = include_html
+
     def emit(self, record):
         try:
             request = record.request
@@ -53,7 +54,7 @@
         except:
             subject = '%s: %s' % (
                 record.levelname,
-                record.msg
+                record.getMessage()
             )
             request = None
             request_repr = "Request repr() unavailable."
@@ -62,7 +63,7 @@
             exc_info = record.exc_info
             stack_trace = 
'\n'.join(traceback.format_exception(*record.exc_info))
         else:
-            exc_info = (None, record.msg, None)
+            exc_info = (None, record.getMessage(), None)
             stack_trace = 'No stack trace available'
 
         message = "%s\n\n%s" % (stack_trace, request_repr)

Modified: django/trunk/tests/regressiontests/logging_tests/tests.py
===================================================================
--- django/trunk/tests/regressiontests/logging_tests/tests.py   2011-08-31 
08:07:07 UTC (rev 16714)
+++ django/trunk/tests/regressiontests/logging_tests/tests.py   2011-09-02 
03:04:02 UTC (rev 16715)
@@ -4,9 +4,12 @@
 
 from django.conf import compat_patch_logging_config
 from django.test import TestCase
-from django.utils.log import CallbackFilter
+from django.test.utils import override_settings
+from django.utils.log import CallbackFilter, getLogger
+from django.core import mail
 
 
+
 # logging config prior to using filter with mail_admins
 OLD_LOGGING = {
     'version': 1,
@@ -115,3 +118,43 @@
         f.filter("a record")
 
         self.assertEqual(collector, ["a record"])
+
+
+class AdminEmailHandlerTest(TestCase):
+
+    @override_settings(
+            ADMINS=(('whatever admin', '[email protected]'),),
+            EMAIL_SUBJECT_PREFIX='-SuperAwesomeSubject-'
+        )
+    def test_accepts_args(self):
+        """
+        Ensure that user-supplied arguments and the EMAIL_SUBJECT_PREFIX
+        setting are used to compose the email subject.
+        Refs #16736.
+        """
+
+        message = "Custom message that says '%s' and '%s'"
+        token1 = 'ping'
+        token2 = 'pong'
+
+        logger = getLogger('django.request')
+        # Inspired from regressiontests/views/views.py: send_log()
+        # ensuring the AdminEmailHandler does not get filtered out
+        # even with DEBUG=True.
+        admin_email_handler = [
+            h for h in logger.handlers
+            if h.__class__.__name__ == "AdminEmailHandler"
+            ][0]
+        # Backup then override original filters
+        orig_filters = admin_email_handler.filters
+        admin_email_handler.filters = []
+
+        logger.error(message, token1, token2)
+
+        self.assertEqual(len(mail.outbox), 1)
+        self.assertEqual(mail.outbox[0].to, ['[email protected]'])
+        self.assertEqual(mail.outbox[0].subject,
+                         "-SuperAwesomeSubject-ERROR: Custom message that says 
'ping' and 'pong'")
+
+        # Restore original filters
+        admin_email_handler.filters = orig_filters

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to