关闭 SQL 日志记录,同时保留设置。DEBUG?
当 settings.DEBUG=True 时,Django 将 SQL 操作记录到内部缓冲区(无论是否记录到文件)。因为我有一个长时间运行的进程执行大量数据库操作,这导致我的程序的开发模式实例的内存消耗增长得非常快。
我想禁用内部 SQL 日志记录机制,同时为我的开发保留打开的 settings.DEBUG:这可能吗?
Django 版本 1.3.0。
Django logs SQL operations to an internal buffer (whether logging to file or not) when settings.DEBUG=True. Because I have long-running process that does a lot of DB operations, this causes my development-mode instances of the program to grow in memory consumption very quickly.
I would like to disable the internal SQL logging mechanism while leaving settings.DEBUG turned on for my development: is this possible?
Django version 1.3.0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,您可以通过将“空处理程序”分配给名为“django.db.backends”的记录器来安静 SQL 日志记录。我假设您使用 django 新的基于字典的日志记录设置?如果是这样,这个片段应该会让事情变得简单:
更新:也看看布莱恩的答案。我理解“日志记录”是指对每个 sql 语句进行令人恼火的日志记录。 Brian 谈论了每个查询的内部内存日志记录(我想他是对的:-)
Yes, you can quiet the sql logging down by assigning a 'null handler' to the logger named 'django.db.backends'. I assume you use django's new dict-based logging setup? If so, this snippet ought to make it easy:
Update: look at Brian's answer, too. I understood "logging" to mean the irritating logging of every sql statement. Brian talks about the internal memory logging of every query (and I guess he's right :-)
当settings.DEBUG为True时,Django使用CursorDebugWrapper而不是CursorWrapper。这就是将查询附加到 connection.queries 并消耗内存的原因。我会对连接包装器进行猴子修补,以始终使用 CursorWrapper:
将其放置在应用程序早期导入的某个文件中。
像其他人建议的那样禁用日志记录不会解决问题,因为即使日志记录关闭,CursorDebugWrapper 仍然将查询存储在 connection.queries 中。
When settings.DEBUG is True, Django uses CursorDebugWrapper instead of CursorWrapper. This is what appends the queries to connection.queries and consumes memory. I would monkey-patch the connection wrapper to always use CursorWrapper:
Place this in some file that gets imported early in your application.
Disabling logging like others suggest won't fix the problem, because CursorDebugWrapper still stores the queries in connection.queries even if logging is off.
这对我有用(至少对于 Django 1.3.1):
我发现检查 Django 源代码的变量(没有记录),相关行可以在
django/db/backends/__init__.py< /code> (
BaseDatabaseWrapper
类):This worked for me (at least for Django 1.3.1):
I've found that variable inspecting Django source code (it is not documented), the relevant lines are found in
django/db/backends/__init__.py
(BaseDatabaseWrapper
class):如果仍然有兴趣跟踪 SQL 操作以进行调试,您还可以定期清理 connection.queries 列表以回收内存:
If still interested in tracing SQL operations for debugging purposes, you can also clean connection.queries list periodically to reclaim memory:
Django 3.0.7
更改
为
django/db/backends/base/db.py
Django 3.0.7
Change
to
in django/db/backends/base/db.py