Fixed #14406 -- Added a Python 2.4 compatibility to the logging interface. Thanks to Łukasz Rekucki for the report, and to Luke Plant for original patch this was based on.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13989 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2010-10-06 15:02:26 +00:00
parent 57aeb54279
commit fea159282b
10 changed files with 56 additions and 19 deletions

View File

@ -1,12 +1,12 @@
import logging
import sys import sys
from django import http from django import http
from django.core import signals from django.core import signals
from django.utils.encoding import force_unicode from django.utils.encoding import force_unicode
from django.utils.importlib import import_module from django.utils.importlib import import_module
from django.utils.log import getLogger
logger = logging.getLogger('django.request') logger = getLogger('django.request')
class BaseHandler(object): class BaseHandler(object):

View File

@ -1,4 +1,3 @@
import logging
import os import os
from pprint import pformat from pprint import pformat
import sys import sys
@ -10,8 +9,9 @@ from django.core.handlers.base import BaseHandler
from django.core.urlresolvers import set_script_prefix from django.core.urlresolvers import set_script_prefix
from django.utils import datastructures from django.utils import datastructures
from django.utils.encoding import force_unicode, smart_str, iri_to_uri from django.utils.encoding import force_unicode, smart_str, iri_to_uri
from django.utils.log import getLogger
logger = logging.getLogger('django.request') logger = getLogger('django.request')
# NOTE: do *not* import settings (or any module which eventually imports # NOTE: do *not* import settings (or any module which eventually imports

View File

@ -1,4 +1,3 @@
import logging
from pprint import pformat from pprint import pformat
import sys import sys
from threading import Lock from threading import Lock
@ -13,8 +12,9 @@ from django.core.handlers import base
from django.core.urlresolvers import set_script_prefix from django.core.urlresolvers import set_script_prefix
from django.utils import datastructures from django.utils import datastructures
from django.utils.encoding import force_unicode, iri_to_uri from django.utils.encoding import force_unicode, iri_to_uri
from django.utils.log import getLogger
logger = logging.getLogger('django.request') logger = getLogger('django.request')
# See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

View File

@ -1,11 +1,11 @@
import datetime import datetime
import decimal import decimal
import logging
from time import time from time import time
from django.utils.hashcompat import md5_constructor from django.utils.hashcompat import md5_constructor
from django.utils.log import getLogger
logger = logging.getLogger('django.db.backends') logger = getLogger('django.db.backends')
class CursorDebugWrapper(object): class CursorDebugWrapper(object):
def __init__(self, cursor, db): def __init__(self, cursor, db):

View File

@ -1,4 +1,3 @@
import logging
import re import re
from django.conf import settings from django.conf import settings
@ -7,8 +6,9 @@ from django.core.mail import mail_managers
from django.utils.http import urlquote from django.utils.http import urlquote
from django.core import urlresolvers from django.core import urlresolvers
from django.utils.hashcompat import md5_constructor from django.utils.hashcompat import md5_constructor
from django.utils.log import getLogger
logger = logging.getLogger('django.request') logger = getLogger('django.request')
class CommonMiddleware(object): class CommonMiddleware(object):

View File

@ -6,7 +6,6 @@ against request forgeries from other sites.
""" """
import itertools import itertools
import logging
import re import re
import random import random
@ -14,6 +13,7 @@ from django.conf import settings
from django.core.urlresolvers import get_callable from django.core.urlresolvers import get_callable
from django.utils.cache import patch_vary_headers from django.utils.cache import patch_vary_headers
from django.utils.hashcompat import md5_constructor from django.utils.hashcompat import md5_constructor
from django.utils.log import getLogger
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
_POST_FORM_RE = \ _POST_FORM_RE = \
@ -21,7 +21,7 @@ _POST_FORM_RE = \
_HTML_TYPES = ('text/html', 'application/xhtml+xml') _HTML_TYPES = ('text/html', 'application/xhtml+xml')
logger = logging.getLogger('django.request') logger = getLogger('django.request')
# Use the system (hardware-based) random number generator if it exists. # Use the system (hardware-based) random number generator if it exists.
if hasattr(random, 'SystemRandom'): if hasattr(random, 'SystemRandom'):

View File

@ -1,4 +1,5 @@
import logging import logging
import sys
from django.core import mail from django.core import mail
# Make sure a NullHandler is available # Make sure a NullHandler is available
@ -17,10 +18,32 @@ try:
except ImportError: except ImportError:
from django.utils.dictconfig import dictConfig from django.utils.dictconfig import dictConfig
if sys.version_info < (2, 5):
class LoggerCompat(object):
def __init__(self, logger):
self._logger = logger
def __getattr__(self, name):
val = getattr(self._logger, name)
if callable(val):
def _wrapper(*args, **kwargs):
# Python 2.4 logging module doesn't support 'extra' parameter to
# methods of Logger
kwargs.pop('extra', None)
return val(*args, **kwargs)
return _wrapper
else:
return val
def getLogger(name=None):
return LoggerCompat(logging.getLogger(name=name))
else:
getLogger = logging.getLogger
# Ensure the creation of the Django logger # Ensure the creation of the Django logger
# with a null handler. This ensures we don't get any # with a null handler. This ensures we don't get any
# 'No handlers could be found for logger "django"' messages # 'No handlers could be found for logger "django"' messages
logger = logging.getLogger('django') logger = getLogger('django')
if not logger.handlers: if not logger.handlers:
logger.addHandler(NullHandler()) logger.addHandler(NullHandler())
@ -35,7 +58,14 @@ class AdminEmailHandler(logging.Handler):
from django.conf import settings from django.conf import settings
try: try:
request = record.request if sys.version_info < (2,5):
# A nasty workaround required because Python 2.4's logging
# module doesn't support passing in extra context.
# For this handler, the only extra data we need is the
# request, and that's in the top stack frame.
request = record.exc_info[2].tb_frame.f_locals['request']
else:
request = record.request
subject = '%s (%s IP): %s' % ( subject = '%s (%s IP): %s' % (
record.levelname, record.levelname,

View File

@ -10,16 +10,16 @@ except ImportError:
from calendar import timegm from calendar import timegm
from datetime import timedelta from datetime import timedelta
from email.Utils import formatdate from email.Utils import formatdate
import logging
from django.utils.decorators import decorator_from_middleware, available_attrs from django.utils.decorators import decorator_from_middleware, available_attrs
from django.utils.http import parse_etags, quote_etag from django.utils.http import parse_etags, quote_etag
from django.utils.log import getLogger
from django.middleware.http import ConditionalGetMiddleware from django.middleware.http import ConditionalGetMiddleware
from django.http import HttpResponseNotAllowed, HttpResponseNotModified, HttpResponse from django.http import HttpResponseNotAllowed, HttpResponseNotModified, HttpResponse
conditional_page = decorator_from_middleware(ConditionalGetMiddleware) conditional_page = decorator_from_middleware(ConditionalGetMiddleware)
logger = logging.getLogger('django.request') logger = getLogger('django.request')
def require_http_methods(request_method_list): def require_http_methods(request_method_list):

View File

@ -1,9 +1,8 @@
import logging
from django.template import loader, RequestContext from django.template import loader, RequestContext
from django.http import HttpResponse, HttpResponseRedirect, HttpResponsePermanentRedirect, HttpResponseGone from django.http import HttpResponse, HttpResponseRedirect, HttpResponsePermanentRedirect, HttpResponseGone
from django.utils.log import getLogger
logger = logging.getLogger('django.request') logger = getLogger('django.request')
def direct_to_template(request, template, extra_context=None, mimetype=None, **kwargs): def direct_to_template(request, template, extra_context=None, mimetype=None, **kwargs):

View File

@ -411,6 +411,10 @@ Messages to this logger have the following extra context:
* ``request``: The request object that generated the logging * ``request``: The request object that generated the logging
message. message.
.. note::
Due to a limitation in the logging library, this extra
context is not available if you are using Python 2.4.
``django.db.backends`` ``django.db.backends``
~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~
@ -424,6 +428,10 @@ Messages to this logger have the following extra context:
* ``sql``: The SQL statement that was executed. * ``sql``: The SQL statement that was executed.
* ``params``: The parameters that were used in the SQL call. * ``params``: The parameters that were used in the SQL call.
.. note::
Due to a limitation in the logging library, this extra
context is not available if you are using Python 2.4.
Handlers Handlers
-------- --------