关闭 SQL 日志记录,同时保留设置。DEBUG?

发布于 2024-12-10 05:01:25 字数 202 浏览 0 评论 0原文

当 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

残花月 2024-12-17 05:01:25

是的,您可以通过将“空处理程序”分配给名为“django.db.backends”的记录器来安静 SQL 日志记录。我假设您使用 django 新的基于字典的日志记录设置?如果是这样,这个片段应该会让事情变得简单:

    ...
    'handlers': {
        'null': {
            'level': 'DEBUG',
            'class':'logging.NullHandler',
            },
    ...
    'loggers': {
        ... your regular logger 'root' or '' ....
        'django.db.backends': {
            'handlers': ['null'],  # Quiet by default!
            'propagate': False,
            'level':'DEBUG',
            },
    ...

更新:也看看布莱恩的答案。我理解“日志记录”是指对每个 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:

    ...
    'handlers': {
        'null': {
            'level': 'DEBUG',
            'class':'logging.NullHandler',
            },
    ...
    'loggers': {
        ... your regular logger 'root' or '' ....
        'django.db.backends': {
            'handlers': ['null'],  # Quiet by default!
            'propagate': False,
            'level':'DEBUG',
            },
    ...

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

拥醉 2024-12-17 05:01:25

当settings.DEBUG为True时,Django使用CursorDebugWrapper而不是CursorWrapper。这就是将查询附加到 connection.queries 并消耗内存的原因。我会对连接包装器进行猴子修补,以始终使用 CursorWrapper:

from django.conf import settings
from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.backends.utils import CursorWrapper

if settings.DEBUG:
    BaseDatabaseWrapper.make_debug_cursor = lambda self, cursor: CursorWrapper(cursor, self)

将其放置在应用程序早期导入的某个文件中。

像其他人建议的那样禁用日志记录不会解决问题,因为即使日志记录关闭,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:

from django.conf import settings
from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.backends.utils import CursorWrapper

if settings.DEBUG:
    BaseDatabaseWrapper.make_debug_cursor = lambda self, cursor: CursorWrapper(cursor, self)

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.

疾风者 2024-12-17 05:01:25

这对我有用(至少对于 Django 1.3.1):

from django.db import connection
connection.use_debug_cursor = False

我发现检查 Django 源代码的变量(没有记录),相关行可以在 django/db/backends/__init__.py< /code> (BaseDatabaseWrapper 类):

def cursor(self):
    if (self.use_debug_cursor or
        (self.use_debug_cursor is None and settings.DEBUG)):
        cursor = self.make_debug_cursor(self._cursor())
    else:
        cursor = util.CursorWrapper(self._cursor(), self)
    return cursor

This worked for me (at least for Django 1.3.1):

from django.db import connection
connection.use_debug_cursor = False

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

def cursor(self):
    if (self.use_debug_cursor or
        (self.use_debug_cursor is None and settings.DEBUG)):
        cursor = self.make_debug_cursor(self._cursor())
    else:
        cursor = util.CursorWrapper(self._cursor(), self)
    return cursor
夜灵血窟げ 2024-12-17 05:01:25

如果仍然有兴趣跟踪 SQL 操作以进行调试,您还可以定期清理 connection.queries 列表以回收内存:

from django.db import connection

for i in range(start, count, size):
    objects = MyModel.objects.order_by('pk').all()[i:i + size]
    ...
    print connection.queries
    connection.queries = []

If still interested in tracing SQL operations for debugging purposes, you can also clean connection.queries list periodically to reclaim memory:

from django.db import connection

for i in range(start, count, size):
    objects = MyModel.objects.order_by('pk').all()[i:i + size]
    ...
    print connection.queries
    connection.queries = []
一直在等你来 2024-12-17 05:01:25

Django 3.0.7

更改

def queries_logged(self):
    self.force_debug_cursor or settings.DEBUG

def queries_logged(self):
    return False

django/db/backends/base/db.py

Django 3.0.7

Change

def queries_logged(self):
    self.force_debug_cursor or settings.DEBUG

to

def queries_logged(self):
    return False

in django/db/backends/base/db.py

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文