Created
August 30, 2016 16:17
-
-
Save eshirazi/8d011f511a1531a69f6dc8d9e7ebc68d to your computer and use it in GitHub Desktop.
Flask SQLAlchemy - print stack trace for every query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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