Ticket #25373: template_import_logging.diff
File template_import_logging.diff, 3.0 KB (added by , 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 1 1 from collections import defaultdict 2 import logging 2 3 3 4 from django.utils import six 4 5 from django.utils.safestring import mark_safe … … register = Library() 12 13 13 14 BLOCK_CONTEXT_KEY = 'block_context' 14 15 16 logger = logging.getLogger('django.template') 17 15 18 16 19 class ExtendsError(Exception): 17 20 pass … … class IncludeNode(Node): 204 207 return template.render(context.new(values)) 205 208 with context.push(**values): 206 209 return template.render(context) 207 except Exception :210 except Exception as e: 208 211 if context.template.engine.debug: 209 212 raise 213 else: 214 logger.warn('template - Uncaught Exception raised during rendering.', exc_info=e) 210 215 return '' 211 216 212 217 -
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 2 2 3 3 import logging 4 4 5 from django.template import Engine, Variable, VariableDoesNotExist6 from django.test import SimpleTestCase5 from django.template import Engine, RequestContext, Variable, VariableDoesNotExist 6 from django.test import RequestFactory, SimpleTestCase 7 7 8 8 9 9 class TestHandler(logging.Handler): … … class VariableResolveLoggingTests(SimpleTestCase): 69 69 def test_no_log_when_variable_exists(self): 70 70 Variable('article.section').resolve({'article': {'section': 'News'}}) 71 71 self.assertIsNone(self.test_handler.log_record) 72 73 74 class 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 )