-
-
Notifications
You must be signed in to change notification settings - Fork 16.2k
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
Using logging's root logger before app.run() breaks Flask logging #2998
Comments
It seems that using the root logger actually caused a full-stop of Flask logs. Relevant issue upstream: pallets/flask#2998
TL;DR: This behavior is caused by When you use any function from the logging module such as From the logging module (3.7):
In the scenario laid out by @ramnes, the root logger is used first, will have a default level of warning, and will have a stream handler attached to it. Now if we go to
The primary issue lies with
First, in our example, Now, whenever the application logger is asked to emit a message, it will have to defer to the nearest handler in the logging tree, which just so happens to be the incidental handler that we attached the root logger when we used it at the very beginning. In our example, this is a stream handler, which happens to default to stderr. Recall that the root logger defaults to a level of warning. This means that any messages with a debug- or info-level messages that the application logger attempts to emit will be suppressed. In our example, warning messages and above will still be output to stderr. If you call There are a couple of potential quick fixes (these won't solve the problem, only mask it)
For a longer-fix, I would look at patching |
This is absolutely intentional. #2436 Flask previously had behavior where it would clear any existing handlers and insert its own, which is bad behavior for a library and was disruptive to people who were configuring logging. Now it will only insert a handler if logging was not otherwise configured to handle its logs. If you want to use Python's logging, you should configure it very early and not rely on Flask's default behavior. |
Whether this behavior is the resultant of an intentional implementation or not is not the point here. I do agree that #2436 make things much better than just removing any existing handler as Flask used to do, but please understand that the current behavior is very developer-hostile in that particular case. No one, beside someone that perfectly knows Flask internals, would consider |
It's not Flask's logging you're configuring, it's Python's. Open an issue with Python if you find that confusing. There is no 100% solution, I'm not changing this again. |
…ng handlers For the rationale, see: pallets/flask#2998
I do agree that the fault mostly comes from Python here. But still, I could not understand why there was no log at all rather than some logs using the default handler, so I gave a quick look in the code. The reason actually lies in Werkzeug, not Flask. Please consider the following PR for a proposal of changes that would fix this "for free": pallets/werkzeug#1407 |
I see a different behavior. When I configure the root logger before any flask code is executed I still get a handler attached to the "werkzeug" handler that outputs plain text to stderr but leaves the propagate flag on. I have configured my service to output JSON formatted log messages that can be parsed by an ELK stack with the field I want so I can correlate all the log messages from any given operation all through all of the microservices it uses. As a result of the added handler... If I do nothing I get both a JSON line and a plain text line. |
@celvartamfam see the linked PR in the comment above yours. |
Logging configuration should be done for each library that outputs logging, that's just how Python is set up. This is discussed in the documentation. Flask's logging does not affect Werkzeug's. Also, it sounds like you're using the dev server in a production environment, which is the only time werkzeug logs would output. Don't do that. |
On latest PyPI's release (1.0.2), using
logging
's root logger before running Flask breaks its logging.For example, with such a
run.py
file:python run.py
outputs this:and then stops any kind of logging.
The application is running and answering, but you don't get the
Running on http://...
line, nor any request logging.It's worth noting that using the root logger inside a route doesn't break Flask.
The text was updated successfully, but these errors were encountered: