bogtan
bogtan

Reputation: 837

Django: Logging in manage.py causes misleading exception and traceback

In manage.py file I want to send an email to admins each time a management command fails.

The following code

manage.py

import logging
logger = logging.getLogger('management_commands')

try:
    execute_from_command_line(sys.argv)
except Exception as e:
    logger.error('Admin Command Error: %s', ' '.join(sys.argv),
                 exc_info=sys.exc_info())
    raise e

is raising

"The translation infrastructure cannot be initialized before the "

django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time.`

when the actual error in fact was ImportError: No module named django_inlinecss.

My settings for the loggers are

LOGGING = {
    ...
    'handlers': {
        ...
        'mail_admins': {
            'class': 'django.utils.log.AdminEmailHandler',
            'level': 'ERROR',
            'include_html': True
        }
    },
    'loggers': {
        ...
        'management_commands': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True
        }
    }
}

And the first part of the traceback is

File "/usr/lib/python2.7/logging/__init__.py", line 1279, in _log
    self.handle(record)
File "/usr/lib/python2.7/logging/__init__.py", line 1289, in handle
    self.callHandlers(record)
File "/usr/lib/python2.7/logging/__init__.py", line 1329, in callHandlers
    hdlr.handle(record)
File "/usr/lib/python2.7/logging/__init__.py", line 757, in handle
    self.emit(record)
File "/usr/local/lib/python2.7/dist-packages/django/utils/log.py", line 128, in emit
    html_message = reporter.get_traceback_html() if self.include_html else None
File "/usr/local/lib/python2.7/dist-packages/django/views/debug.py", line 384, in get_traceback_html
    return t.render(c)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 210, in render
    return self._render(context)

This is how I ended up thinking there might be a problem logging related.

Any reason why python logging library would make Django raise "The translation infrastructure cannot be initialized before the "?

Upvotes: 2

Views: 335

Answers (2)

John Lehmann
John Lehmann

Reputation: 8225

This error can also happen with this version of manage.py when you've not installed an app that is listed in INSTALLED_APPS.

You can remove the except clause in manage.py so the exception is displayed without fancy rendering, avoiding the confusing error.

Upvotes: 0

bruno desthuilliers
bruno desthuilliers

Reputation: 77912

You have configured the "mail_admins" logger to include html, which triggers execution of some template rendering in debug views, which itself requires django's translation system to be initialized.

The simplest solution here would be to configure another handler without the "include_html" flag and use this one instead for your logger, ie (warning : totally untested):

# logging config

LOGGING = {
    ...
    'handlers': {
        ...
        'mail_admins': {
            'class': 'django.utils.log.AdminEmailHandler',
            'level': 'ERROR',
            'include_html': True
        },
        'command_mail_admins': {
            'class': 'django.utils.log.AdminEmailHandler',
            'level': 'ERROR',
            'include_html': False
        },
    },
    'loggers': {
        ...
        'management_commands': {
            'handlers': ['command_mail_admins'],
            'level': 'ERROR',
            'propagate': True
        }
    }
}

Upvotes: 2

Related Questions