Ticket #25373: template_import_logging.diff

File template_import_logging.diff, 3.0 KB (added by Nick Johnson, 9 years ago)
  • django/template/loader_tags.py

    diff --git a/django/template/loader_tags.py b/django/template/loader_tags.py
    index 1d6d922..a710dcc 100644
    a b  
    11from collections import defaultdict
     2import logging
    23
    34from django.utils import six
    45from django.utils.safestring import mark_safe
    register = Library()  
    1213
    1314BLOCK_CONTEXT_KEY = 'block_context'
    1415
     16logger = logging.getLogger('django.template')
     17
    1518
    1619class ExtendsError(Exception):
    1720    pass
    class IncludeNode(Node):  
    204207                return template.render(context.new(values))
    205208            with context.push(**values):
    206209                return template.render(context)
    207         except Exception:
     210        except Exception as e:
    208211            if context.template.engine.debug:
    209212                raise
     213            else:
     214                logger.warn('template - Uncaught Exception raised during rendering.', exc_info=e)
    210215            return ''
    211216
    212217
  • tests/template_tests/test_logging.py

    diff --git a/tests/template_tests/test_logging.py b/tests/template_tests/test_logging.py
    index f6a9bcb..7e784bc 100644
    a b from __future__ import unicode_literals  
    22
    33import logging
    44
    5 from django.template import Engine, Variable, VariableDoesNotExist
    6 from django.test import SimpleTestCase
     5from django.template import Engine, RequestContext, Variable, VariableDoesNotExist
     6from django.test import RequestFactory, SimpleTestCase
    77
    88
    99class TestHandler(logging.Handler):
    class VariableResolveLoggingTests(SimpleTestCase):  
    6969    def test_no_log_when_variable_exists(self):
    7070        Variable('article.section').resolve({'article': {'section': 'News'}})
    7171        self.assertIsNone(self.test_handler.log_record)
     72
     73
     74class ExceptionDuringRenderLoggingTests(SimpleTestCase):
     75    def setUp(self):
     76        self.test_handler = TestHandler()
     77        self.logger = logging.getLogger('django.template')
     78        self.original_level = self.logger.level
     79        self.logger.addHandler(self.test_handler)
     80        self.logger.setLevel(logging.WARN)
     81
     82    def tearDown(self):
     83        self.logger.removeHandler(self.test_handler)
     84        self.logger.level = self.original_level
     85
     86    def test_logs_exceptions_during_rendering_with_debug_disabled(self):
     87        engine = Engine(loaders=[
     88            ('django.template.loaders.locmem.Loader', {
     89                'child': '{% for var in varlist %}{{ var.somemethod }}{% endfor %}',
     90            }),
     91        ], debug=False)
     92        request = RequestFactory().get('/')
     93
     94        class ErrorObj():
     95            def somemethod(self):
     96                raise IndexError("some generic exception")
     97
     98        always_raises_exception = {'varlist': [ErrorObj()]}
     99
     100        ctx = RequestContext(request, always_raises_exception)
     101        self.assertEqual(engine.from_string('{% include "child" %}').render(ctx), '')
     102        self.assertEqual(
     103            self.test_handler.log_record.msg,
     104            'template - Uncaught Exception raised during rendering.'
     105        )
Back to Top