Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django channels can't log exceptions in console

Django-Channels can't show exception error in console When an exception raise in any section of my consumer.

it just show "WebSocket DISCONNECT /ws/test/ [127.0.0.1:7907]" in console when exception occurred.

I run project with debug=True setting with this command:

python3 manage.py runserver

I use basic settings for django-channles:

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

and have a very simple consumer for test:

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)

and routing:

application = ProtocolTypeRouter({
    'websocket': AuthMiddlewareStack(
        URLRouter(
            orders.routing.websocket_urlpatterns
        )
    ),
})
like image 702
Alex Mercer Avatar asked Jul 03 '19 15:07

Alex Mercer


1 Answers

I found the problem. Django channels can't log exceptions in console when i use debug_toolbar app. I removed debug_toolbar from INSTALLED_APPS and then the problem was solved.

this is my debug_toolbar configs:

# 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']
like image 174
Alex Mercer Avatar answered Oct 22 '22 19:10

Alex Mercer