Opened 3 years ago

Closed 3 months ago

#18773 closed New feature (fixed)

Enable optional logging of invalid variable lookups

Reported by: wrigley Owned by: CarolineSimpson
Component: Template system Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Adding something like

# django/template/base.py
if getattr(settings,'TEMPLATE_LOG_IF_INVALID', False):
    logging.getLogger(settings.TEMPLATE_LOG_IF_INVALID).debug(
        "Failed lookup for key in [%s] in %r" %(bit,current))

around line 763, or whenever the silent_variable_failure is tested or something, would be awesome! That way there would be an easy way to check for invalid variables in the templates without compromising neither adminpages, layout or page reliability.

Then you could use something like:

# settings.py
TEMPLATE_LOG_IF_INVALID = 'invalid_log'

LOGGING = {
    'version': 1,
    'handlers': {
        'console': {
            'class': 'logger.StreamHandler',
            'level': 'DEBUG'
        }
    },
    'loggers': {
        'invalid_log': {
            'handlers': 'console',
            'level': 'DEBUG'
        }
    }
}

a loglevel of WARNING might be more appropriate, but I'll leave that decision up to you guys, seeing as I am new to django and don't know neither the code base nor the conventions too well yet.

Cheers,
/J

Change History (13)

comment:1 Changed 3 years ago by claudep

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 15 months ago by CarolineSimpson

  • Has patch set
  • Owner changed from nobody to CarolineSimpson
  • Status changed from new to assigned

I've submitted a pull request for this issue: https://github.com/django/django/pull/2552

Added a new logger, and log when there is an exception in the resolve function. The logger uses the null handler by default.

comment:3 Changed 15 months ago by CarolineSimpson

The pull request has been updated to include the template name in the output.

comment:4 Changed 11 months ago by timgraham

  • Patch needs improvement set

I left comments for improvement on the PR. Please uncheck "Patch needs improvement" when you update it, thanks.

comment:5 Changed 6 months ago by CarolineSimpson

  • Patch needs improvement unset

I have submitted a new pull request with the updates requested for the previous pull request.

comment:6 Changed 6 months ago by CarolineSimpson

I've updated the pull request with fixes from review feedback.

comment:7 Changed 6 months ago by berkerpeksag

  • Version changed from 1.4 to master

comment:8 Changed 4 months ago by timgraham

  • Patch needs improvement set

Reviewed PR & needs a rebase.

comment:9 Changed 4 months ago by CarolineSimpson

  • Patch needs improvement unset

I've updated the patch and done a rebase.

comment:10 Changed 4 months ago by CarolineSimpson

  • Patch needs improvement set

comment:11 Changed 3 months ago by timgraham

  • Patch needs improvement unset
  • Triage Stage changed from Accepted to Ready for checkin

Looks good, pending some minor cosmetic changes.

comment:12 Changed 3 months ago by CarolineSimpson

The latest review changes are now incorporated into the patch as well.

comment:13 Changed 3 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In dc5b01a:

Fixed #18773 -- Added logging for template variable resolving

Added a django.template logger without a default handler. Added
logging if there is an exception while resolving variables in a
template.

Note: See TracTickets for help on using tickets.
Back to Top