I'd like to print the code a particular session generates and sends to the DB. I've seen this question but it echoes all the SQL that SQLAlchemy generates. I want just the SQL generated on behalf of a particular session. Bonus points if it can be printed before it is actually send to the DB (like we can with the Query object)
I know I can print the SQL of a query, but in this situation
a = Table(id=1)
session.add(a)
b = Table(id=4)
session.add(a)
b.column = 5
session.commit()
there is no query to print, but the session is beginning a transaction, inserting rows and committing the transaction.
I'd like the session to print to stdout (or a file, if that's not possible) the SQL commands it's sending to the DB.
The (immaginary) SQL I expect to find in the console is
BEGIN TRANSACTION
INSERT INTO Table (id) VALUES (1);
INSERT INTO Table (id, column) VALUES (4,5);
COMMIT TRANSACTION
I fully understand that the lines might not all come together and might be interleaved by other code prints (the session is beginning the transaction first, then later the code trigger the flush of the insert, and as last thing the session issues the commit)
Is that possible?