Mercurial > kallithea
comparison pylons_app/lib/timerproxy.py @ 153:a5a3bcc5ee89
Added colored formatter to project, and configs
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Sun, 16 May 2010 15:06:20 +0200 |
parents | 0c22a870bb79 |
children | 83c7ee1b5f5c |
comparison
equal
deleted
inserted
replaced
152:0c00fbaff55a | 153:a5a3bcc5ee89 |
---|---|
1 from sqlalchemy.interfaces import ConnectionProxy | 1 from sqlalchemy.interfaces import ConnectionProxy |
2 import time | 2 import time |
3 import logging | 3 import logging |
4 log = logging.getLogger('timerproxy') | 4 log = logging.getLogger('timerproxy') |
5 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38) | |
6 | |
7 def color_sql(sql): | |
8 COLOR_SEQ = "\033[1;%dm" | |
9 COLOR_SQL = YELLOW | |
10 normal = '\x1b[0m' | |
11 return COLOR_SEQ % COLOR_SQL + sql + normal | |
12 | |
13 def format_sql(sql): | |
14 sql = color_sql(sql) | |
15 sql = sql.replace('SELECT', '\n SELECT \n\t')\ | |
16 .replace('FROM', '\n FROM')\ | |
17 .replace('ORDER BY', '\n ORDER BY')\ | |
18 .replace('LIMIT', '\n LIMIT')\ | |
19 .replace('WHERE', '\n WHERE')\ | |
20 .replace('AND', '\n AND')\ | |
21 .replace('LEFT', '\n LEFT')\ | |
22 .replace('INNER', '\n INNER')\ | |
23 .replace('INSERT', '\n INSERT')\ | |
24 .replace('DELETE', '\n DELETE') | |
25 return sql | |
26 | |
5 | 27 |
6 class TimerProxy(ConnectionProxy): | 28 class TimerProxy(ConnectionProxy): |
7 def cursor_execute(self, execute, cursor, statement, parameters, context, executemany): | 29 def cursor_execute(self, execute, cursor, statement, parameters, context, executemany): |
8 now = time.time() | 30 now = time.time() |
9 try: | 31 try: |
10 log.info(">>>>> STARTING QUERY >>>>>") | 32 log.info(">>>>> STARTING QUERY >>>>>") |
11 return execute(cursor, statement, parameters, context) | 33 return execute(cursor, statement, parameters, context) |
12 finally: | 34 finally: |
13 total = time.time() - now | 35 total = time.time() - now |
14 try: | 36 try: |
15 log.info("Query: %s" % statement % parameters) | 37 log.info(format_sql("Query: %s" % statement % parameters)) |
16 except TypeError: | 38 except TypeError: |
17 log.info("Query: %s %s" % (statement, parameters)) | 39 log.info(format_sql("Query: %s %s" % (statement, parameters))) |
18 log.info("<<<<< TOTAL TIME: %f <<<<<" % total) | 40 log.info("<<<<< TOTAL TIME: %f <<<<<" % total) |
41 | |
42 | |
43 | |
44 |