Added support for logging of undefined objects.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
fixup! fixup!
authored by Armin Ronacher · committed by Armin Ronacher · parent e7a9b94 · Browse files · 1 file·+41 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+41 added−6 removed
- other:+41 added−6 removed
Provenance
- Commit
d2641be- Tree
e1b6478- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
e7a9b94
1 file changed+41 added−6 removed
modified:jinja2/runtime.py+41 added-6 removed
6 unmodified lines7 8 :copyright: (c) 2010 by the Jinja Team.9 :license: BSD.10 """11+import sys12+13 from itertools import chain14 from jinja2.nodes import EvalContext, _context_function_types15 from jinja2.utils import Markup, soft_unicode, escape, missing, concat, \16 internalcode, object_type_repr6 unmodified lines23 # these variables are exported to the template runtime24 __all__ = ['LoopContext', 'TemplateReference', 'Macro', 'Markup',25 'TemplateRuntimeError', 'missing', 'concat', 'escape',26 'markup_join', 'unicode_join', 'to_string', 'identity',25- 'TemplateNotFound']27+ 'TemplateNotFound', 'make_logging_undefined']28 29 #: the name of the function that is used to convert something into30 #: a string. We can just use the text type here.31 to_string = text_type140 unmodified lines172 argument if the callable is a :func:`contextfunction` or173 :func:`environmentfunction`.174 """175 if __debug__:174- __traceback_hide__ = True176+ __traceback_hide__ = True # noqa177 178 # Allow callable classes to take a context179 fn = __obj.__call__180 for fn_type in ('contextfunction',312 unmodified lines493 raise AttributeError(name)494 return self._fail_with_undefined_error()495 496 __add__ = __radd__ = __mul__ = __rmul__ = __div__ = __rdiv__ = \495- __truediv__ = __rtruediv__ = __floordiv__ = __rfloordiv__ = \496- __mod__ = __rmod__ = __pos__ = __neg__ = __call__ = \497- __getitem__ = __lt__ = __le__ = __gt__ = __ge__ = __int__ = \498- __float__ = __complex__ = __pow__ = __rpow__ = \497+ __truediv__ = __rtruediv__ = __floordiv__ = __rfloordiv__ = \498+ __mod__ = __rmod__ = __pos__ = __neg__ = __call__ = \499+ __getitem__ = __lt__ = __le__ = __gt__ = __ge__ = __int__ = \500+ __float__ = __complex__ = __pow__ = __rpow__ = \501 _fail_with_undefined_error502 503 def __eq__(self, other):504 return type(self) is type(other)20 unmodified lines525 def __repr__(self):526 return 'Undefined'527 528 529+def make_logging_undefined(logger=None, base=None):530+ """Given a logger object this returns a new undefined class that531+ will log all failures into it. If no logger is given a default532+ logger is created.533+ """534+ if logger is None:535+ import logging536+ logger = logging.getLogger(__name__)537+ logger.addHandler(logging.StreamHandler(sys.stderr))538+ if base is None:539+ base = Undefined540+541+ class LoggingUndefined(base):542+ def __str__(self):543+ if self._undefined_hint is None:544+ if self._undefined_obj is missing:545+ hint = '%s is undefined' % self._undefined_name546+ elif not isinstance(self._undefined_name, string_types):547+ hint = '%s has no element %s' % (548+ object_type_repr(self._undefined_obj),549+ self._undefined_name)550+ else:551+ hint = '%s has no attribute %s' % (552+ object_type_repr(self._undefined_obj),553+ self._undefined_name)554+ else:555+ hint = self._undefined_hint556+ logger.error('Template error: %s', hint)557+ return base.__str__(self)558+559+ return LoggingUndefined560+561+562 @implements_to_string563 class DebugUndefined(Undefined):564 """An undefined that returns the debug info when printed.565 52 unmodified lines