Source code for galaxy.model.orm.logging_connection_proxy

import inspect
import logging
import os
import threading
import time

from sqlalchemy.interfaces import ConnectionProxy

log = logging.getLogger(__name__)

wd = os.getcwd()


[docs]def stripwd(s): if s.startswith(wd): return s[len(wd):] return s
[docs]def pretty_stack(): rval = [] for frame, fname, line, funcname, _, _ in inspect.stack()[2:]: rval.append("%s:%s@%d" % (stripwd(fname), funcname, line)) return rval
[docs]class LoggingProxy(ConnectionProxy): """ Logs SQL statements using standard logging module """
[docs] def begin(self, conn, begin): thread_ident = threading.current_thread().ident begin(conn) log.debug("begin transaction: thread: %r" % thread_ident)
[docs] def commit(self, conn, commit): thread_ident = threading.current_thread().ident commit(conn) log.debug("commit transaction: thread: %r" % thread_ident)
[docs] def rollback(self, conn, rollback): thread_ident = threading.current_thread().ident rollback(conn) log.debug("rollback transaction: thread: %r" % thread_ident)
[docs] def cursor_execute(self, execute, cursor, statement, parameters, context, executemany): thread_ident = threading.current_thread().ident start = time.clock() rval = execute(cursor, statement, parameters, context) duration = time.clock() - start log.debug("statement: %r parameters: %r executemany: %r duration: %r stack: %r thread: %r", statement, parameters, executemany, duration, " > ".join(pretty_stack()), thread_ident) return rval
[docs]class TraceLoggerProxy(ConnectionProxy): """ Logs SQL statements using a metlog client """
[docs] def __init__(self, trace_logger): self.trace_logger = trace_logger
[docs] def cursor_execute(self, execute, cursor, statement, parameters, context, executemany): start = time.clock() rval = execute(cursor, statement, parameters, context) duration = time.clock() - start self.trace_logger.log( "sqlalchemy_query", message="Query executed", statement=statement, parameters=parameters, executemany=executemany, duration=duration ) return rval