Changeset - a5a17000e45b
[Not reviewed]
celery
0 1 0
Marcin Kuzminski - 15 years ago 2010-09-21 15:36:46
marcin@python-works.com
timeproxy logging memory leak fix.
1 file changed with 12 insertions and 11 deletions:
0 comments (0 inline, 0 general)
pylons_app/lib/timerproxy.py
Show inline comments
 
from sqlalchemy.interfaces import ConnectionProxy
 
import time
 
import logging
 
log = logging.getLogger('timerproxy')
 
from sqlalchemy import log
 
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38)
 

	
 
def color_sql(sql):
 
    COLOR_SEQ = "\033[1;%dm"
 
    COLOR_SQL = YELLOW
 
    normal = '\x1b[0m'
 
@@ -19,13 +18,12 @@ def one_space_trim(s):
 
    
 
def format_sql(sql):
 
    sql = color_sql(sql)
 
    sql = sql.replace('\n', '')
 
    sql = one_space_trim(sql)
 
    sql = sql\
 
        .replace(',',',\n\t')\
 
        .replace('SELECT', '\n\tSELECT \n\t')\
 
        .replace('UPDATE', '\n\tUPDATE \n\t')\
 
        .replace('DELETE', '\n\tDELETE \n\t')\
 
        .replace('FROM', '\n\tFROM')\
 
        .replace('ORDER BY', '\n\tORDER BY')\
 
        .replace('LIMIT', '\n\tLIMIT')\
 
@@ -36,22 +34,25 @@ def format_sql(sql):
 
        .replace('INSERT', '\n\tINSERT')\
 
        .replace('DELETE', '\n\tDELETE')
 
    return sql
 

	
 

	
 
class TimerProxy(ConnectionProxy):
 
    
 
    def __init__(self):
 
        super(TimerProxy, self).__init__()
 
        self.logging_name = 'timerProxy'
 
        self.log = log.instance_logger(self, True)
 
        
 
    def cursor_execute(self, execute, cursor, statement, parameters, context, executemany):
 
        
 
        now = time.time()
 
        try:
 
            log.info(">>>>> STARTING QUERY >>>>>")
 
            self.log.info(">>>>> STARTING QUERY >>>>>")
 
            return execute(cursor, statement, parameters, context)
 
        finally:
 
            total = time.time() - now
 
            try:
 
                log.info(format_sql("Query: %s" % statement % parameters))
 
                self.log.info(format_sql("Query: %s" % statement % parameters))
 
            except TypeError:
 
                log.info(format_sql("Query: %s %s" % (statement, parameters)))
 
            log.info("<<<<< TOTAL TIME: %f <<<<<" % total)
 

	
 

	
 

	
 

	
 
                self.log.info(format_sql("Query: %s %s" % (statement, parameters)))
 
            self.log.info("<<<<< TOTAL TIME: %f <<<<<" % total)
0 comments (0 inline, 0 general)