Ticket #16736: 16736.adminemailhandler-formatted-subject.diff

File 16736.adminemailhandler-formatted-subject.diff, 2.8 KB (added by julien, 4 years ago)
  • django/utils/log.py

    diff -r 7fe93a3d9442 django/utils/log.py
    a b  
    5353        except:
    5454            subject = '%s: %s' % (
    5555                record.levelname,
    56                 record.msg
     56                record.getMessage()
    5757            )
    5858            request = None
    5959            request_repr = "Request repr() unavailable."
     
    6262            exc_info = record.exc_info
    6363            stack_trace = '\n'.join(traceback.format_exception(*record.exc_info))
    6464        else:
    65             exc_info = (None, record.msg, None)
     65            exc_info = (None, record.getMessage(), None)
    6666            stack_trace = 'No stack trace available'
    6767
    6868        message = "%s\n\n%s" % (stack_trace, request_repr)
  • tests/regressiontests/logging_tests/tests.py

    diff -r 7fe93a3d9442 tests/regressiontests/logging_tests/tests.py
    a b  
    44
    55from django.conf import compat_patch_logging_config
    66from django.test import TestCase
    7 from django.utils.log import CallbackFilter
     7from django.test.utils import override_settings
     8from django.utils.log import CallbackFilter, getLogger
     9from django.core import mail
     10
    811
    912
    1013# logging config prior to using filter with mail_admins
     
    115118        f.filter("a record")
    116119
    117120        self.assertEqual(collector, ["a record"])
     121
     122
     123class AdminEmailHandlerTest(TestCase):
     124
     125    @override_settings(ADMINS=(('whatever admin', 'admin@example.com'),))
     126    def test_accepts_args(self):
     127        """
     128        Ensure that user-supplied arguments are merged into the log record's
     129        message.
     130        Refs #16736.
     131        """
     132
     133        message = "This is my message, and it says '%s' and '%s'"
     134        token1 = 'ping'
     135        token2 = 'pong'
     136
     137        logger = getLogger('django.request')
     138        # Taken from regressiontests/views/views.py: send_log()
     139        # ensuring the AdminEmailHandler does not get filtered out
     140        # even with DEBUG=True.
     141        admin_email_handler = [
     142            h for h in logger.handlers
     143            if h.__class__.__name__ == "AdminEmailHandler"
     144            ][0]
     145        # Backup then override original filters
     146        orig_filters = admin_email_handler.filters
     147        admin_email_handler.filters = []
     148
     149        logger.error(message, token1, token2)
     150
     151        self.assertEqual(len(mail.outbox), 1)
     152        self.assertEqual(mail.outbox[0].to, ['admin@example.com'])
     153        self.assertEqual(mail.outbox[0].subject,
     154                         "[Django] ERROR: This is my message, and it says 'ping' and 'pong'")
     155
     156        # Restore original filters
     157        admin_email_handler.filters = orig_filters
Back to Top