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

查看:113
本文介绍了记录所有sql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何记录我的django应用程序执行的所有SQL查询?



我想记录所有内容,包括管理站点的SQL。我看到此问题一个FAQ答案,但我仍然无法弄清楚我应该从哪里选择

 从django.db导入连接
connection.queries

将所有内容记录到一个文件?



所以我的问题是 - 我应该怎么做一个文件(比如all-sql.log)记录所有的SQL语句?

解决方案

请参阅 https://github.com/django-debug-toolbar/ django-debug-toolbar



它将让您看到给定页面生成的所有查询。以及它们出现的堆栈跟踪等。



编辑:将所有SQL查询记录到文件等,然后您将创建一些中间件。中间件可以在每个请求上运行。在这里有几个Django片段:





那些关心打印到终端,但并不难适应他们使用python的日志库。


How can I log all SQL queries that my django application performed?

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?

So my question is - what should I do to have a file (say all-sql.log) where all SQL statements are logged?

解决方案

Maybe check out 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.

EDIT: 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:

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天全站免登陆