Skip to content
Myr

Error Handling documentation fixes (grammar, etc)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Adam Zapletal · committed by Armin Ronacher · parent 505c530 · Browse files · 1 file+17 added−16 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+17 added−16 removed

  • other:+17 added−16 removed

1 file+17 added−16 removed

modified:docs/errorhandling.rst+17 added-16 removed
3 unmodified lines
4===========================56.. versionadded:: 0.378Applications fail, server fail. Sooner or later you will see an exception8Applications fail, servers fail. Sooner or later you will see an exception9in production. Even if your code is 100% correct, you will still see10exceptions from time to time. Why? Because everything else involved will11fail. Here some situations where perfectly fine code can lead to server12errors:
6 unmodified lines
19- a backend server overloaded20- a programming error in a library you are using21- network connection of the server to another system failed.2223And that's just a small sample of issues you could be facing. So how to23And that's just a small sample of issues you could be facing. So how do we24deal with that sort of problem? By default if your application runs in25production mode, Flask will display a very simple page for you and log the26exception to the :attr:`~flask.Flask.logger`.27
3 unmodified lines
31Error Mails32-----------3334If the application runs in production mode (which it will do on your35server) you won't see any log messages by default. Why that? Flask tries36to be a zero-configuration framework and where should it drop the logs for37you if there is no configuration. Guessing is not a good idea because38chances are, the place it guessed is not the place where the user has the35server) you won't see any log messages by default. Why is that? Flask36tries to be a zero-configuration framework. Where should it drop the logs37for you if there is no configuration? Guessing is not a good idea because38chances are, the place it guessed is not the place where the user has39permission to create a logfile. Also, for most small applications nobody40will look at the logs anyways.4142In fact, I promise you right now that if you configure a logfile for the
1 unmodified line
44when a user reported it for you. What you want instead is a mail the45second the exception happened. Then you get an alert and you can do46something about it.4748Flask is using the Python builtin logging system and that one can actually49send you mails for errors which is probably what you want. Here is how50you can configure the Flask logger to send you mails for exceptions::48Flask uses the Python builtin logging system, and it can actually send49you mails for errors which is probably what you want. Here is how you can50configure the Flask logger to send you mails for exceptions::5152 ADMINS = ['yourname@example.com']53 if not app.debug:54 import logging
7 unmodified lines
62So what just happened? We created a new63:class:`~logging.handlers.SMTPHandler` that will send mails with the mail64server listening on ``127.0.0.1`` to all the `ADMINS` from the address65*server-error@example.com* with the subject "YourApplication Failed". If66your mail server requires credentials these can also provided, for that67check out the documentation for the :class:`~logging.handlers.SMTPHandler`.66your mail server requires credentials, these can also be provided. For67that check out the documentation for the68:class:`~logging.handlers.SMTPHandler`.6970We also tell the handler to only send errors and more critical messages.71Because we certainly don't want to get a mail for warnings or other72useless logs that might happen during request handling.
42 unmodified lines
115Controlling the Log Format116--------------------------117118By default a handler will only write the message string into a file or118send you that message as mail. But a log record stores more information119send you that message as mail. A log record stores more information,120and it makes a lot of sense to configure your logger to also contain that121information so that you have a better idea of why that error happened, and122more importantly, where it did.123123A formatter can be instanciated with a format string. Note that124A formatter can be instantiated with a format string. Note that125tracebacks are appended to the log entry automatically. You don't have to126do that in the log formatter format string.127128Here some example setups:
77 unmodified lines
206:meth:`~logging.Formatter.formatTime`:207 called for `asctime` formatting. If you want a different time format208 you can override this method.209:meth:`~logging.Formatter.formatException`209 called for exception formatting. It is passed a :attr:`~sys.exc_info`210 called for exception formatting. It is passed an :attr:`~sys.exc_info`211 tuple and has to return a string. The default is usually fine, you212 don't have to override it.213214For more information, head over to the official documentation.
2 unmodified lines
217Other Libraries218---------------219220So far we only configured the logger your application created itself.220Other libraries might log themselves as well. For example, SQLAlchemy use221logging heavily in the core. While there is a method to configure all221Other libraries might log themselves as well. For example, SQLAlchemy uses222logging heavily in its core. While there is a method to configure all223loggers at once in the :mod:`logging` package, I would not recommend using224it. There might be a situation in which you want to have multiple225separate applications running side by side in the same Python interpreter226and then it becomes impossible to have different logging setups for those.
11 unmodified lines