changeset 15:c8265b4f0c2a

added log4j to development,
author Marcin Kuzminski
date Sun, 21 Feb 2010 14:24:50 +0100
parents 923f0e6ab010
children e121401a17d8
files development.ini hgwebdir.config pylons_app/lib/profiler.py
diffstat 3 files changed, 66 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/development.ini	Sat Feb 20 22:45:59 2010 +0100
+++ b/development.ini	Sun Feb 21 14:24:50 2010 +0100
@@ -45,16 +45,16 @@
 keys = root, routes, pylons_app, sqlalchemy
 
 [handlers]
-keys = console
+keys = console,chainsaw
 
 [formatters]
-keys = generic
+keys = generic,xmllayout
 
 #############
 ## LOGGERS ##
 #############
 [logger_root]
-level = INFO
+level = NOTSET
 handlers = console
 
 [logger_routes]
@@ -84,6 +84,12 @@
 level = NOTSET
 formatter = generic
 
+[handler_chainsaw]
+class = xmllayout.RawSocketHandler
+args = ('localhost', 4448)
+level = NOTSET
+formatter = xmllayout
+
 ################
 ## FORMATTERS ##
 ################
@@ -92,3 +98,6 @@
 format = %(asctime)s,%(msecs)03d %(levelname)-5.5s [%(name)s] %(message)s
 datefmt = %H:%M:%S
 
+[formatter_xmllayout]
+class = xmllayout.XMLLayout
+
--- a/hgwebdir.config	Sat Feb 20 22:45:59 2010 +0100
+++ b/hgwebdir.config	Sun Feb 21 14:24:50 2010 +0100
@@ -8,7 +8,7 @@
 
 [web]
 push_ssl = false
-contact = marcin.kuzminski@etelko.pl
+contact = develop@etelko.pl
 allow_archive = gz zip bz2
 allow_push = *
 style = gitweb
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pylons_app/lib/profiler.py	Sun Feb 21 14:24:50 2010 +0100
@@ -0,0 +1,53 @@
+from __future__ import with_statement
+
+import cProfile
+import pstats
+import cgi
+import pprint
+import threading
+
+from StringIO import StringIO
+
+class ProfilingMiddleware(object):
+    def __init__(self, app):
+        self.lock = threading.Lock()
+        self.app = app
+    
+    
+    def __call__(self, environ, start_response):
+        with self.lock:
+            profiler = cProfile.Profile()
+            def run_app(*a, **kw):
+                self.response = self.app(environ, start_response)
+
+            profiler.runcall(run_app, environ, start_response)
+
+            profiler.snapshot_stats()
+
+            stats = pstats.Stats(profiler)
+            stats.sort_stats('cumulative')
+
+            # Redirect output
+            out = StringIO()
+            stats.stream = out
+
+            stats.print_stats()
+
+            resp = ''.join(self.response)
+
+            # Lets at least only put this on html-like responses.
+            if resp.strip().startswith('<'):
+                ## The profiling info is just appended to the response.
+                ##  Browsers don't mind this.
+                resp += '<pre style="text-align:left; border-top: 4px dashed red; padding: 1em;">'
+                resp += cgi.escape(out.getvalue(), True)
+                
+                output = StringIO()
+                pprint.pprint(environ, output, depth=3)
+                
+                resp += cgi.escape(output.getvalue(), True)
+                resp += '</pre>'
+                
+            return resp
+    
+