Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Daphne disables warnings module #352

Open
mjnaderi opened this issue Jan 23, 2021 · 9 comments · May be fixed by django/channels#1620
Open

Daphne disables warnings module #352

mjnaderi opened this issue Jan 23, 2021 · 9 comments · May be fixed by django/channels#1620

Comments

@mjnaderi
Copy link

mjnaderi commented Jan 23, 2021

Hi. I'm using django-channels in a django project. I wanted to use Python's warnings module to display a warning message. But it didn't work. Nothing was printed. I started the server with python -Wa manage.py runserver, but the warning didn't show up.

My code:

import warnings

def myview(request):
    warnings.warn("you've been warned!")
    ...

I removed channels from INSTALLED_APPS and ran runserver again. Issue was gone and the message was printed.

I tracked down the issue to this line in daphne code (server.py):

# Redirect the Twisted log to nowhere
globalLogBeginner.beginLoggingTo(
    [lambda _: None], redirectStandardIO=False, discardBuffer=True
)

In beginLoggingTo, twisted runs this code:

self._warningsModule.showwarning = self.showwarning

which replaces warnings.showwarning with a custom function. (When I comment this line in twisted, issue is fixed.)

Would you please show me how can I use Python's warnings module properly?

@mjnaderi
Copy link
Author

Versions:

daphne: 3.0.1
channels: 3.0.2
django: 3.1.4
twisted: 20.3.0

@carltongibson
Copy link
Member

Hi @mjnaderi — initial ideal would be to run with verbosity 1 (or greater).

@mjnaderi
Copy link
Author

mjnaderi commented Jan 23, 2021

Hi @carltongibson. I'm running the project using manage.py runserver, and channels.management.commands.runserver doesn't pass verbosity argument to Server, so it runs with default verbosity=1 (verbosity >=2 is required to fix the issue).

I think this issue can make a lot of confusion for users. It's changing the default behavior of warnings module. All runtime warning messages from all packages (even Django itself!) are lost, and it's hard to find the reason.

@mjnaderi
Copy link
Author

I tried increasing verbosity (by manually modifying server.py). Increasing verbosity alone can't solve the issue. I needed to modify Django's LOGGING to make it work.

LOGGING = {
    ...
    "loggers": {
        "daphne": {
            "handlers": ["console"]
        }
    },
}

@carltongibson
Copy link
Member

OK, so at the least we could do with some docs here. Passing verbosity would also be worthwhile. I need to have a look into WHY the behaviour is as it is before deciding how/if we can change it. Happy if you want to have a dig though!

Thanks for the report!

mjnaderi added a commit to mjnaderi/channels that referenced this issue Jan 23, 2021
@mjnaderi
Copy link
Author

I did some experiment on this. I didn't find out why twisted redirects warnings to log messages. But I implemented a simple work-around: mjnaderi/channels@13fbe8a

@carltongibson
Copy link
Member

Hi @mjnaderi — glad you found a workaround. If you want to make a PR to Channel's runserver that would be very welcome.

@mjnaderi
Copy link
Author

Thanks @carltongibson
I opened a PR: django/channels#1620

@mjnaderi
Copy link
Author

@carltongibson any update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

2 participants