annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
1 from sqlalchemy.interfaces import ConnectionProxy
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
2 import time
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
3 import logging
90
0c22a870bb79 logging proxy update
Marcin Kuzminski <marcin@python-works.com>
parents: 49
diff changeset
4 log = logging.getLogger('timerproxy')
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
5 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = xrange(30, 38)
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
6
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
7 def color_sql(sql):
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
8 COLOR_SEQ = "\033[1;%dm"
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
9 COLOR_SQL = YELLOW
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
10 normal = '\x1b[0m'
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
11 return COLOR_SEQ % COLOR_SQL + sql + normal
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
12
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
13 def format_sql(sql):
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
14 sql = color_sql(sql)
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
15 sql = sql.replace('SELECT', '\n SELECT \n\t')\
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
16 .replace('FROM', '\n FROM')\
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
17 .replace('ORDER BY', '\n ORDER BY')\
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
18 .replace('LIMIT', '\n LIMIT')\
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
19 .replace('WHERE', '\n WHERE')\
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
20 .replace('AND', '\n AND')\
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
21 .replace('LEFT', '\n LEFT')\
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
22 .replace('INNER', '\n INNER')\
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
23 .replace('INSERT', '\n INSERT')\
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
24 .replace('DELETE', '\n DELETE')
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
25 return sql
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
26
49
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
27
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
28 class TimerProxy(ConnectionProxy):
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
29 def cursor_execute(self, execute, cursor, statement, parameters, context, executemany):
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
30 now = time.time()
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
31 try:
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
32 log.info(">>>>> STARTING QUERY >>>>>")
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
33 return execute(cursor, statement, parameters, context)
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
34 finally:
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
35 total = time.time() - now
90
0c22a870bb79 logging proxy update
Marcin Kuzminski <marcin@python-works.com>
parents: 49
diff changeset
36 try:
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
37 log.info(format_sql("Query: %s" % statement % parameters))
90
0c22a870bb79 logging proxy update
Marcin Kuzminski <marcin@python-works.com>
parents: 49
diff changeset
38 except TypeError:
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
39 log.info(format_sql("Query: %s %s" % (statement, parameters)))
49
3ada2f409c1c Added sqlalchemy support
Marcin Kuzminski <marcin@python-blog.com>
parents:
diff changeset
40 log.info("<<<<< TOTAL TIME: %f <<<<<" % total)
153
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
41
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
42
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
43
a5a3bcc5ee89 Added colored formatter to project, and configs
Marcin Kuzminski <marcin@python-works.com>
parents: 90
diff changeset
44