Code

Ticket #16736: adminemail_getmessage_patch.diff

File adminemail_getmessage_patch.diff, 2.3 KB (added by strioni, 3 years ago)

This is the final patch, ignore the other ones.

Line 
1Index: django/utils/log.py
2===================================================================
3--- django/utils/log.py (revision 16714)
4+++ django/utils/log.py (working copy)
5@@ -53,7 +53,7 @@
6         except:
7             subject = '%s: %s' % (
8                 record.levelname,
9-                record.msg
10+                record.getMessage()
11             )
12             request = None
13             request_repr = "Request repr() unavailable."
14@@ -62,7 +62,7 @@
15             exc_info = record.exc_info
16             stack_trace = '\n'.join(traceback.format_exception(*record.exc_info))
17         else:
18-            exc_info = (None, record.msg, None)
19+            exc_info = (None, record.getMessage(), None)
20             stack_trace = 'No stack trace available'
21 
22         message = "%s\n\n%s" % (stack_trace, request_repr)
23Index: tests/regressiontests/logging_tests/tests.py
24===================================================================
25--- tests/regressiontests/logging_tests/tests.py        (revision 16714)
26+++ tests/regressiontests/logging_tests/tests.py        (working copy)
27@@ -115,3 +115,36 @@
28         f.filter("a record")
29 
30         self.assertEqual(collector, ["a record"])
31+
32+
33+class AdminEmailHandlerTest(TestCase):
34+    def test_accepts_args(self):
35+        from django.conf import settings
36+        from django.core import mail
37+        from django.utils.log import getLogger, logging
38+
39+        old_admins = settings.ADMINS
40+        settings.ADMINS = (('whatever admin', 'admin@example.com'),)
41+
42+        message = "this is my message, and it says '%s'"
43+        token = 'token'
44+
45+        logger = getLogger('django.request')
46+        # taken from regressiontests/views/views.py: send_log
47+        # ensuring the AdminEmailHandler does not get filtered out
48+        # even with DEBUG=True.
49+        admin_email_handler = [
50+            h for h in logger.handlers
51+            if h.__class__.__name__ == "AdminEmailHandler"
52+            ][0]
53+        orig_filters = admin_email_handler.filters
54+        admin_email_handler.filters = []
55+
56+        logger.error(message, token)
57+
58+        self.assertEqual(len(mail.outbox), 1)
59+        self.assertTrue('token' in mail.outbox[0].subject,
60+            "Subject should contain the token")
61+
62+        admin_email_handler.filters = orig_filters
63+        settings.ADMINS = old_admins