comparison pylons_app/lib/timerproxy.py @ 492:a5a17000e45b celery

timeproxy logging memory leak fix.
author Marcin Kuzminski <marcin@python-works.com>
date Tue, 21 Sep 2010 15:36:46 +0200
parents 298546182b41
children b4d9680cd164
comparison
equal deleted inserted replaced
491:fefffd6fd5f4 492:a5a17000e45b
1 from sqlalchemy.interfaces import ConnectionProxy 1 from sqlalchemy.interfaces import ConnectionProxy
2 import time 2 import time
3 import logging 3 from sqlalchemy import log
4 log = logging.getLogger('timerproxy')
5 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38) 4 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38)
6 5
7 def color_sql(sql): 6 def color_sql(sql):
8 COLOR_SEQ = "\033[1;%dm" 7 COLOR_SEQ = "\033[1;%dm"
9 COLOR_SQL = YELLOW 8 COLOR_SQL = YELLOW
20 def format_sql(sql): 19 def format_sql(sql):
21 sql = color_sql(sql) 20 sql = color_sql(sql)
22 sql = sql.replace('\n', '') 21 sql = sql.replace('\n', '')
23 sql = one_space_trim(sql) 22 sql = one_space_trim(sql)
24 sql = sql\ 23 sql = sql\
25 .replace(',',',\n\t')\
26 .replace('SELECT', '\n\tSELECT \n\t')\ 24 .replace('SELECT', '\n\tSELECT \n\t')\
27 .replace('UPDATE', '\n\tUPDATE \n\t')\ 25 .replace('UPDATE', '\n\tUPDATE \n\t')\
28 .replace('DELETE', '\n\tDELETE \n\t')\ 26 .replace('DELETE', '\n\tDELETE \n\t')\
29 .replace('FROM', '\n\tFROM')\ 27 .replace('FROM', '\n\tFROM')\
30 .replace('ORDER BY', '\n\tORDER BY')\ 28 .replace('ORDER BY', '\n\tORDER BY')\
37 .replace('DELETE', '\n\tDELETE') 35 .replace('DELETE', '\n\tDELETE')
38 return sql 36 return sql
39 37
40 38
41 class TimerProxy(ConnectionProxy): 39 class TimerProxy(ConnectionProxy):
40
41 def __init__(self):
42 super(TimerProxy, self).__init__()
43 self.logging_name = 'timerProxy'
44 self.log = log.instance_logger(self, True)
45
42 def cursor_execute(self, execute, cursor, statement, parameters, context, executemany): 46 def cursor_execute(self, execute, cursor, statement, parameters, context, executemany):
47
43 now = time.time() 48 now = time.time()
44 try: 49 try:
45 log.info(">>>>> STARTING QUERY >>>>>") 50 self.log.info(">>>>> STARTING QUERY >>>>>")
46 return execute(cursor, statement, parameters, context) 51 return execute(cursor, statement, parameters, context)
47 finally: 52 finally:
48 total = time.time() - now 53 total = time.time() - now
49 try: 54 try:
50 log.info(format_sql("Query: %s" % statement % parameters)) 55 self.log.info(format_sql("Query: %s" % statement % parameters))
51 except TypeError: 56 except TypeError:
52 log.info(format_sql("Query: %s %s" % (statement, parameters))) 57 self.log.info(format_sql("Query: %s %s" % (statement, parameters)))
53 log.info("<<<<< TOTAL TIME: %f <<<<<" % total) 58 self.log.info("<<<<< TOTAL TIME: %f <<<<<" % total)
54
55
56
57