Skip to content

Instantly share code, notes, and snippets.

@eshirazi
Created August 30, 2016 16:17
Show Gist options
  • Save eshirazi/8d011f511a1531a69f6dc8d9e7ebc68d to your computer and use it in GitHub Desktop.
Save eshirazi/8d011f511a1531a69f6dc8d9e7ebc68d to your computer and use it in GitHub Desktop.
Flask SQLAlchemy - print stack trace for every query
import sqlalchemy.event
def show_query_stack_traces():
base_path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
def catch_queries_callback(connection, cursor, query, *_):
print query
stacktrace = [
(file_path, val1, val2, line_contents)
for (file_path, val1, val2, line_contents) in traceback.extract_stack()
if file_path.startswith(base_path)
]
for i in stacktrace:
print "\t", i
print "\n"
sqlalchemy.event.listen(db.engine, "before_cursor_execute", catch_queries_callback)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment