记录所有 sql 查询
如何记录我的 django 应用程序执行的所有 SQL 查询?
How can I log all SQL queries that my django application performed?
我想记录所有内容,包括来自管理站点的 SQL.我看到了这个问题和常见问题解答 但我还是不知道该放在哪里
I want to log everything, including SQLs from admin site. I saw this question and a FAQ answer but I still can't figure out where should I put
from django.db import connection
connection.queries
要将所有内容都记录到一个文件中?
to log everything to one file?
所以我的问题是 - 我应该怎么做才能拥有一个记录所有 SQL 语句的文件(比如 all-sql.log)?
So my question is - what should I do to have a file (say all-sql.log) where all SQL statements are logged?
也许可以看看 https://github.com/django-debug-toolbar/django-debug-toolbar
它会让您看到给定页面生成的所有查询.以及它们发生的地方的堆栈跟踪等.
It'll let you see all the queries generated by a given page. As well as stacktraces of where they occur etc.
将所有 SQL 查询记录到文件等中,然后您需要创建一些中间件.中间件在每个请求上运行.有几个 Django 片段可以用于此类事情:
to log all SQL queries to a file etc, then you will want to create some middleware. Middleware gets run on every request. There are several Django snippets out there for this sort of thing:
那些与打印到终端有关,但使它们适应使用 python 的日志库并不难.
Those are concerned with printing to the terminal, but it wouldn't be hard to adapt them to use python's logging library.