-
-
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
How should logging in Flask look like? #2023
Comments
What would happen to the log messages in production, when the current flask logger would be moved to development only? |
The same thing that already happens: you need to add your own logging handler in order to see messages and exceptions generated in production. |
Why does that already happen? On 13 September 2016 10:56:52 CEST, David Lord [email protected] wrote:
Sent from my Android device with K-9 Mail. Please excuse my brevity. |
|
Python takes a modular approach to its logging by separating the concerns into loggers, handlers, filters, and formatters. Maybe it would be good to have a very simple, yet explicit, default setup for each of these classes with some documentation on how to easily override it, just as flask does for the request and response classes. |
That is actually the current state. The problem arises when people use The question is whether it's a good idea to even have any logging setup by default as it may interfere with the plans of the app developer. On 14 September 2016 03:44:50 CEST, Ryan Baker [email protected] wrote:
Sent from my Android device with K-9 Mail. Please excuse my brevity. |
Er, my suggestion was a little different than the current state, actually. I was suggesting that, much like the app has a Are you talking about any logging that a developer might want to do outside of the context of their flask app? |
I hope I'm not polluting the conversation by adding https://www.bountysource.com/issues/85851-design-issue-with-flask-logger-create_logger |
No. Why should it? If I want to enable logging, I can just
Wouldn't it be enough to just mention in the docs that you must initialize logging yourself, as usual, be it through |
I spend some days around this, trying to figure out how to implement logging on a Flask app. At the end I decide that, at the current state, the best is to avoid the use of the Flask logger. Some problems I've found wit the custom Flask's logger:
How should logging in Flask look like? Do we even want Flask to do any logging setup? From my point of view, there should be to parts for logging in Flask: One part for the library/framework, with a high-level logger node hardcoded to "flask" or "pallets.flask" and child loggers. This will allow 'instrument' the library as #1572 suggests, and also be used by third party libraries like flask-login, flask-wtf, etc. Other part, for the Flask app, with a configurable name. To avoid that the reference to the logger could change the name should be pass as an argument to Flask() constructor, e.g.: This should be reflected in the docs stating that the preferred way is that the developer configure the logging system and the default config should only be used as a last resort. To allow this the custom formatters and handlers/filters should be exposed to allow import when configuring logging, as @Rdbaker suggests above. I think that find a way that the Flask's log system goes along with the Logging facility for Python, and not against, is important enough to try to target this discussion to 1.0. I could help with some code or docs when decisions are made. |
Trying to learn more about logging I've come across this: http://plumberjack.blogspot.com.es/2010/09/python-logging-functionality-facts-vs.html and I realize why Flask want to pass the logger object around. Perhaps the first question to make is what logging library is wanted to use by Flask and use it properly. Any option is perfectly fine, but using the stdlib logging as if it was other thing only leads to confusion. |
For one thing, Flask should log chained exceptions. Maybe somebody here would be kind enough to answer my StackOverflow question asking how to get Flask to do this? @tinproject: Your comment didn't 'confuse' me; I just used the ☹ emoji because it's sad that configuring Flask's logs is apparently difficult. Changing the log format is the first thing I'll want do after My App Works Well Enough, mostly for the above reason, and also to get rid of all the dashes and But apparently some people like them, which is fair enough, and a reason logs should be easily configurable. My personal preference would be for a blank line and maybe some dashes each time Flask reloads the app, and for log lines to be timestamped. |
Libraries should not configure logging, they should just log and let the application author configure destinations to their liking. That's best practice for python logging:
|
It'd be important to note that if the recommended practice was the path taken (i.e. add a |
I believe NullHandler is just an optional convenience, not necessary. Py 2.6 was EOL'd 5 years ago. Though I'm not in a hurry to break it, so checking beforehand couldn't hurt. BTW, this should be straightened out before a 1.0 release, is there a tag for that? |
I wouldn't mind seeing 2.6 support go away once Flask reaches v1.0 ;) |
+1 to following the best practice for Python logging. I don't mind what Flask logs per se but would prefer I had control of the handlers myself since |
I've been looking at this, and "follow best practice" is not as straightforward as it sounds, unfortunately.
Things I've been experimenting with:
|
@davidism, these complex use cases may not be 100% solvable as you are noticing. That's the main reason to leave it up to the (end) developer rather than trying to outsmart them. (Ask me how I know, haha. I have a kivy app where a non-insignificant amount of code is there to defeat and disable their mandatory logging configuration meant for my own good.) By all means have a document detailing how to log this and configure that with a list of provided logger names. But when the developer googles "enable logging in a python app" let the standard answer suffice. I think a "hands off" approach fits better with Flask's breezy design as well. |
I've created #2436 that hopefully simplifies Flask's logging configuration. It also splits out and updates the documentation to help clarify how everything works. If anyone here wants to take a look and make sure it addresses the points brought up here, I'd appreciate it. |
Flask started to ship with a default, hardcoded logging handler. Unfortunately this setup makes it harder to install custom logging setups, because then you'll have to undo all the things Flask did to the app logger, or replace the
app.logger
entirely. A symptom of this is #1993, where Flask's own logger had to be tweaked yet again such that messages didn't get logged twice (once via Flask's setup, once via the custom one).My question is: Do we even want Flask to do any logging setup? It appears that this sort of default logging is only useful during development, so maybe it makes sense to set up a default logging handler in the new Flask CLI instead of from within the application.
The text was updated successfully, but these errors were encountered: