Is logging all sql queries possible with DEBUG=False (staging environment)?

Hi everyone. We’re using a Wagtail CMS that’s built on top of Django framework.
Recently we’ve experienced an error which only manifested itself at the staging environment.
We need to intercept all sql queries at our staging site. Currently this has been solved with the help of SaaS-based MySql monitoring service. But it would be awesome to be able to catch not only the query but the module/caller information which isn’t provided by the current interception mechanism ↑.
I’ve found such a workaround: django - Signal/Method for every executed sql statement - Stack Overflow, but it’s raw and I’m not sure if it remains reliable in the future.
Is it achievable - to log all SQL queries with DEBUG=False as with DEBUG=True via the LOGGING config?

Yes, I’m sure it’s possible - I’m just not sure of the specifics necessary to do so.

I’m guessing the easiest way may be to override the DatabaseWrapper class to set the force_debug_cursor attribute True. (See Database instrumentation at a higher layer as an example. Note: You can ignore all the stuff related to the SQL compiler. I believe that all you may need is to define the DatabaseWrapper class, override the attribute, and set that class as the engine.)

You might also be able to do this by importing your Database Engine class in the settings file and monkey-patch it.

1 Like