Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14406 -- Added a Python 2.4 compatibility to the logging inter…

…face. 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
  • Loading branch information...
commit fea159282b75130b5f6cf52ec92ddfa09467f3c9 1 parent 57aeb54
Russell Keith-Magee authored October 06, 2010
4  django/core/handlers/base.py
... ...
@@ -1,12 +1,12 @@
1  
-import logging
2 1
 import sys
3 2
 
4 3
 from django import http
5 4
 from django.core import signals
6 5
 from django.utils.encoding import force_unicode
7 6
 from django.utils.importlib import import_module
  7
+from django.utils.log import getLogger
8 8
 
9  
-logger = logging.getLogger('django.request')
  9
+logger = getLogger('django.request')
10 10
 
11 11
 
12 12
 class BaseHandler(object):
4  django/core/handlers/modpython.py
... ...
@@ -1,4 +1,3 @@
1  
-import logging
2 1
 import os
3 2
 from pprint import pformat
4 3
 import sys
@@ -10,8 +9,9 @@
10 9
 from django.core.urlresolvers import set_script_prefix
11 10
 from django.utils import datastructures
12 11
 from django.utils.encoding import force_unicode, smart_str, iri_to_uri
  12
+from django.utils.log import getLogger
13 13
 
14  
-logger = logging.getLogger('django.request')
  14
+logger = getLogger('django.request')
15 15
 
16 16
 
17 17
 # NOTE: do *not* import settings (or any module which eventually imports
4  django/core/handlers/wsgi.py
... ...
@@ -1,4 +1,3 @@
1  
-import logging
2 1
 from pprint import pformat
3 2
 import sys
4 3
 from threading import Lock
@@ -13,8 +12,9 @@
13 12
 from django.core.urlresolvers import set_script_prefix
14 13
 from django.utils import datastructures
15 14
 from django.utils.encoding import force_unicode, iri_to_uri
  15
+from django.utils.log import getLogger
16 16
 
17  
-logger = logging.getLogger('django.request')
  17
+logger = getLogger('django.request')
18 18
 
19 19
 
20 20
 # See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
4  django/db/backends/util.py
... ...
@@ -1,11 +1,11 @@
1 1
 import datetime
2 2
 import decimal
3  
-import logging
4 3
 from time import time
5 4
 
6 5
 from django.utils.hashcompat import md5_constructor
  6
+from django.utils.log import getLogger
7 7
 
8  
-logger = logging.getLogger('django.db.backends')
  8
+logger = getLogger('django.db.backends')
9 9
 
10 10
 class CursorDebugWrapper(object):
11 11
     def __init__(self, cursor, db):
4  django/middleware/common.py
... ...
@@ -1,4 +1,3 @@
1  
-import logging
2 1
 import re
3 2
 
4 3
 from django.conf import settings
@@ -7,8 +6,9 @@
7 6
 from django.utils.http import urlquote
8 7
 from django.core import urlresolvers
9 8
 from django.utils.hashcompat import md5_constructor
  9
+from django.utils.log import getLogger
10 10
 
11  
-logger = logging.getLogger('django.request')
  11
+logger = getLogger('django.request')
12 12
 
13 13
 
14 14
 class CommonMiddleware(object):
4  django/middleware/csrf.py
@@ -6,7 +6,6 @@
6 6
 """
7 7
 
8 8
 import itertools
9  
-import logging
10 9
 import re
11 10
 import random
12 11
 
@@ -14,6 +13,7 @@
14 13
 from django.core.urlresolvers import get_callable
15 14
 from django.utils.cache import patch_vary_headers
16 15
 from django.utils.hashcompat import md5_constructor
  16
+from django.utils.log import getLogger
17 17
 from django.utils.safestring import mark_safe
18 18
 
19 19
 _POST_FORM_RE = \
@@ -21,7 +21,7 @@
21 21
 
22 22
 _HTML_TYPES = ('text/html', 'application/xhtml+xml')
23 23
 
24  
-logger = logging.getLogger('django.request')
  24
+logger = getLogger('django.request')
25 25
 
26 26
 # Use the system (hardware-based) random number generator if it exists.
27 27
 if hasattr(random, 'SystemRandom'):
34  django/utils/log.py
... ...
@@ -1,4 +1,5 @@
1 1
 import logging
  2
+import sys
2 3
 from django.core import mail
3 4
 
4 5
 # Make sure a NullHandler is available
@@ -17,10 +18,32 @@ def emit(self, record):
17 18
 except ImportError:
18 19
     from django.utils.dictconfig import dictConfig
19 20
 
  21
+if sys.version_info < (2, 5):
  22
+    class LoggerCompat(object):
  23
+        def __init__(self, logger):
  24
+            self._logger = logger
  25
+
  26
+        def __getattr__(self, name):
  27
+            val = getattr(self._logger, name)
  28
+            if callable(val):
  29
+                def _wrapper(*args, **kwargs):
  30
+                    # Python 2.4 logging module doesn't support 'extra' parameter to
  31
+                    # methods of Logger
  32
+                    kwargs.pop('extra', None)
  33
+                    return val(*args, **kwargs)
  34
+                return _wrapper
  35
+            else:
  36
+                return val
  37
+
  38
+    def getLogger(name=None):
  39
+        return LoggerCompat(logging.getLogger(name=name))
  40
+else:
  41
+    getLogger = logging.getLogger
  42
+
20 43
 # Ensure the creation of the Django logger
21 44
 # with a null handler. This ensures we don't get any
22 45
 # 'No handlers could be found for logger "django"' messages
23  
-logger = logging.getLogger('django')
  46
+logger = getLogger('django')
24 47
 if not logger.handlers:
25 48
     logger.addHandler(NullHandler())
26 49
 
@@ -35,7 +58,14 @@ def emit(self, record):
35 58
         from django.conf import settings
36 59
 
37 60
         try:
38  
-            request = record.request
  61
+            if sys.version_info < (2,5):
  62
+                # A nasty workaround required because Python 2.4's logging
  63
+                # module doesn't support passing in extra context.
  64
+                # For this handler, the only extra data we need is the
  65
+                # request, and that's in the top stack frame.
  66
+                request = record.exc_info[2].tb_frame.f_locals['request']
  67
+            else:
  68
+                request = record.request
39 69
 
40 70
             subject = '%s (%s IP): %s' % (
41 71
                 record.levelname,
4  django/views/decorators/http.py
@@ -10,16 +10,16 @@
10 10
 from calendar import timegm
11 11
 from datetime import timedelta
12 12
 from email.Utils import formatdate
13  
-import logging
14 13
 
15 14
 from django.utils.decorators import decorator_from_middleware, available_attrs
16 15
 from django.utils.http import parse_etags, quote_etag
  16
+from django.utils.log import getLogger
17 17
 from django.middleware.http import ConditionalGetMiddleware
18 18
 from django.http import HttpResponseNotAllowed, HttpResponseNotModified, HttpResponse
19 19
 
20 20
 conditional_page = decorator_from_middleware(ConditionalGetMiddleware)
21 21
 
22  
-logger = logging.getLogger('django.request')
  22
+logger = getLogger('django.request')
23 23
 
24 24
 
25 25
 def require_http_methods(request_method_list):
5  django/views/generic/simple.py
... ...
@@ -1,9 +1,8 @@
1  
-import logging
2  
-
3 1
 from django.template import loader, RequestContext
4 2
 from django.http import HttpResponse, HttpResponseRedirect, HttpResponsePermanentRedirect, HttpResponseGone
  3
+from django.utils.log import getLogger
5 4
 
6  
-logger = logging.getLogger('django.request')
  5
+logger = getLogger('django.request')
7 6
 
8 7
 
9 8
 def direct_to_template(request, template, extra_context=None, mimetype=None, **kwargs):
8  docs/topics/logging.txt
@@ -411,6 +411,10 @@ Messages to this logger have the following extra context:
411 411
     * ``request``: The request object that generated the logging
412 412
       message.
413 413
 
  414
+.. note::
  415
+    Due to a limitation in the logging library, this extra
  416
+    context is not available if you are using Python 2.4.
  417
+
414 418
 ``django.db.backends``
415 419
 ~~~~~~~~~~~~~~~~~~~~~~
416 420
 
@@ -424,6 +428,10 @@ Messages to this logger have the following extra context:
424 428
     * ``sql``: The SQL statement that was executed.
425 429
     * ``params``: The parameters that were used in the SQL call.
426 430
 
  431
+.. note::
  432
+    Due to a limitation in the logging library, this extra
  433
+    context is not available if you are using Python 2.4.
  434
+
427 435
 Handlers
428 436
 --------
429 437
 

0 notes on commit fea1592

Please sign in to comment.
Something went wrong with that request. Please try again.