Login

debug info middleware

Author:
mzitnik
Posted:
August 17, 2008
Language:
Python
Version:
.96
Tags:
middleware django debug information
Score:
1 (after 1 ratings)

Hi,

I have developed a middleware that enables to view debugging information in production for a single user filtered by useragent or ip. The debug info is appended to the html code as a remark and can be viewed with a view source operation from the browser.

Take a look at http://code.google.com/p/debugview/

Enjoy

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# look at http://code.google.com/p/debugview/
from django.conf import settings
# default
_DEFAULT_HTML_TYPES = ('text/html','text/plain')

_DEFAULT_DEBUG_USER_AGENT = 'DEBUG'
_INCLUDE_URL = None
_EXCLUDE_URL = None
_IP_FILTER = None


# try to get the list from settings.py 
# if want to change the list
try:
    _HTML_TYPES = settings.HTML_TYPES
except Exception ,e:
    _HTML_TYPES = _DEFAULT_HTML_TYPES

# get user agent from settings file if does not exists use default
try:
    _DEBUG_USER_AGENT = settings.DEBUG_USER_AGENT
except Exception ,e:
    _DEBUG_USER_AGENT = None #_DEFAULT_DEBUG_USER_AGENT

try:
    _IP_FILTER = settings.DEBUG_IP_LIST
except Exception ,e:
    _IP_FILTER = None

def rendering_method(debug_info=None):
    if debug_info == None: return ''
    return '<!--'+str(debug_info) + ' -->'

_DEFAULT_RENDERING_METHOD = rendering_method

try:
    _RENDERING_METHOD = settings.RENDERING_METHOD
except Exception ,e:
    _RENDERING_METHOD = _DEFAULT_RENDERING_METHOD 

'''

	Append Debug information logic

	* check content type (from specified list)
	* check if user agent containe _DEBUG_USER_AGENT if _DEBUG_USER_AGENT == None do not perform this check
	*
	*
'''


class DebugView:
    def process_response(self, request, response):
        # check client user_agent
        if response['Content-Type'].split(';')[0] not in _HTML_TYPES: return response    
        
	agent = client_ip = url = None
	#print request.META
	if _DEBUG_USER_AGENT != None: 
		agent = request.META.get('HTTP_USER_AGENT',None)
		if agent.find(_DEBUG_USER_AGENT) == -1: return response

	if _IP_FILTER != None: 
            # check client ip 
	    client_ip = request.META.get('REMOTE_ADDR',None)
            if client_ip not in _IP_FILTER: return response
	    print 'client ip was found in the ip filter list'

	if _INCLUDE_URL != None or _EXCLUDE_URL != None: 
            url = request.get_full_path() # call method that gets url
	    # here we need to check url 

	# check if variables got values
	if agent == None and client_ip == None and url == None: return response
	print 'Agent' , agent , 'IP' , client_ip , 'URL' ,url 

	
        try:
            debug_info = request.DEBUG
        except Exception ,e:
	    # debug info not found just return the response
            return response
	# run method if defined to create the string that will be added as debug info 
        #response.content = response.content + rendering_method(debug_info)
	
	try:
            response.content = response.content + _RENDERING_METHOD(debug_info)  
	except Exception ,e:
	    print e
        return response

def append_debug_info(request,key,value):
    debug = None
    try:
        debug = request.DEBUG
    except Exception ,e:
        # init dic for debug info
        debug = request.DEBUG = {} 
    debug[key] = value

More like this

  1. another request logging middleware with request time and extra info by yoav 3 years, 8 months ago
  2. Firebug Lite Middleware by jfw 6 years, 10 months ago
  3. WebFaction fixes middleware by spookylukey 6 years ago
  4. Security: Sideband information cover traffic middleware by jdunck 5 years, 5 months ago
  5. Request time logging middleware by mpasternacki 5 years, 9 months ago

Comments

Please login first before commenting.