PageRenderTime 6ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/galaxy/model/orm/logging_connection_proxy.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 48 lines | 35 code | 7 blank | 6 comment | 3 complexity | b654c720bb3341c027f73b2c62a6d3d1 MD5 | raw file
 1import time
 2import inspect
 3import os
 4
 5from galaxy.model.orm import *
 6
 7import logging
 8
 9log = logging.getLogger( __name__ )
10
11wd = os.getcwd()
12def stripwd( s ):
13    if s.startswith( wd ):
14        return s[len(wd):]
15    return s
16
17def pretty_stack():
18    rval = []
19    for frame, fname, line, funcname, _, _ in inspect.stack()[2:]:
20        rval.append( "%s:%s@%d" % ( stripwd( fname ), funcname, line ) )
21    return rval
22
23class LoggingProxy(ConnectionProxy):
24    """
25    Logs SQL statements using standard logging module
26    """
27    def cursor_execute(self, execute, cursor, statement, parameters, context, executemany):
28        start = time.clock()
29        rval = execute(cursor, statement, parameters, context)
30        duration = time.clock() - start
31        log.debug( "statement: %r parameters: %r executemany: %r duration: %r stack: %r",
32                   statement, parameters, executemany, duration, " > ".join( pretty_stack() ) )
33        return rval
34
35class TraceLoggerProxy(ConnectionProxy):
36    """
37    Logs SQL statements using a metlog client
38    """
39    def __init__( self, trace_logger ):
40        self.trace_logger = trace_logger
41    def cursor_execute(self, execute, cursor, statement, parameters, context, executemany):
42        start = time.clock()
43        rval = execute(cursor, statement, parameters, context)
44        duration = time.clock() - start
45        self.trace_logger.log( "sqlalchemy_query",
46            message="Query executed", statement=statement, parameters=parameters,
47            executemany=executemany, duration=duration )
48        return rval