记录所有 sql 查询 [英] log all sql queries

查看:37
本文介绍了记录所有 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.

这篇关于记录所有 sql 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆