Django 频道无法在控制台中记录异常

Django channels can't log exceptions in console

当我的消费者的任何部分出现异常时,Django-Channels 无法在控制台中显示异常错误。

当发生异常时,它只在控制台中显示 "WebSocket DISCONNECT /ws/test/ [127.0.0.1:7907]"。

我 运行 使用此命令设置 debug=True 的项目:

python3 manage.py runserver

我使用 django-channles 的基本设置:

# CHANNELS
# ------------------------------------------------------------------------------
ASGI_APPLICATION = "config.routing.application"
CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            "hosts": [('127.0.0.1', 6379)],
        },
    },
}

并有一个非常简单的消费者进行测试:

class Test(WebsocketConsumer):
  def connect(self):
    print(self.scope['headers'])  # show headers in console.
    raise Exception('test exception')  # this line doesn't show any thing in console. just show disconnect message.
    self.accept()

  def receive(self, text_data=None, bytes_data=None):
    print(text_data)

和路由:

application = ProtocolTypeRouter({
    'websocket': AuthMiddlewareStack(
        URLRouter(
            orders.routing.websocket_urlpatterns
        )
    ),
})

我发现了问题。当我使用 debug_toolbar 应用程序时,Django 频道无法在控制台中记录异常。我从INSTALLED_APPS中删除了debug_toolbar,然后问题就解决了。

这是我的 debug_toolbar 配置:

# django-debug-toolbar
# ------------------------------------------------------------------------------
# https://django-debug-toolbar.readthedocs.io/en/latest/installation.html#prerequisites
INSTALLED_APPS += ['debug_toolbar']  # noqa F405
# https://django-debug-toolbar.readthedocs.io/en/latest/installation.html#middleware
MIDDLEWARE += ['debug_toolbar.middleware.DebugToolbarMiddleware']  # noqa F405
# https://django-debug-toolbar.readthedocs.io/en/latest/configuration.html#debug-toolbar-config
DEBUG_TOOLBAR_CONFIG = {
    'DISABLE_PANELS': [
        'debug_toolbar.panels.redirects.RedirectsPanel',
    ],
    'SHOW_TEMPLATE_CONTEXT': True,
}
# https://django-debug-toolbar.readthedocs.io/en/latest/installation.html#internal-ips
INTERNAL_IPS = ['127.0.0.1', '10.0.2.2']