Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 303 lines (237 sloc) 12.534 kb
267f5f5 Armin Ronacher Added a pattern for custom error pages.
authored
1 .. _application-errors:
2
69e7a0a Ron DuPlain Move debugger details into a new section, #343.
rduplain authored
3 Logging Application Errors
4 ==========================
ea5e654 Armin Ronacher Added a documentation chapter about logging
authored
5
ce6e4cb Armin Ronacher 0.5 is 0.3 now, why skip numbers?
authored
6 .. versionadded:: 0.3
ea5e654 Armin Ronacher Added a documentation chapter about logging
authored
7
ee5eafa Adam Zapletal Error Handling documentation fixes (grammar, etc)
adamzap authored
8 Applications fail, servers fail. Sooner or later you will see an exception
ea5e654 Armin Ronacher Added a documentation chapter about logging
authored
9 in production. Even if your code is 100% correct, you will still see
10 exceptions from time to time. Why? Because everything else involved will
11 fail. Here some situations where perfectly fine code can lead to server
12 errors:
13
14 - the client terminated the request early and the application was still
15 reading from the incoming data.
16 - the database server was overloaded and could not handle the query.
17 - a filesystem is full
18 - a harddrive crashed
19 - a backend server overloaded
20 - a programming error in a library you are using
21 - network connection of the server to another system failed.
22
ee5eafa Adam Zapletal Error Handling documentation fixes (grammar, etc)
adamzap authored
23 And that's just a small sample of issues you could be facing. So how do we
ea5e654 Armin Ronacher Added a documentation chapter about logging
authored
24 deal with that sort of problem? By default if your application runs in
25 production mode, Flask will display a very simple page for you and log the
26 exception to the :attr:`~flask.Flask.logger`.
27
28 But there is more you can do, and we will cover some better setups to deal
29 with errors.
30
31 Error Mails
32 -----------
33
34 If the application runs in production mode (which it will do on your
ee5eafa Adam Zapletal Error Handling documentation fixes (grammar, etc)
adamzap authored
35 server) you won't see any log messages by default. Why is that? Flask
36 tries to be a zero-configuration framework. Where should it drop the logs
37 for you if there is no configuration? Guessing is not a good idea because
38 chances are, the place it guessed is not the place where the user has
ea5e654 Armin Ronacher Added a documentation chapter about logging
authored
39 permission to create a logfile. Also, for most small applications nobody
40 will look at the logs anyways.
41
42 In fact, I promise you right now that if you configure a logfile for the
43 application errors you will never look at it except for debugging an issue
44 when a user reported it for you. What you want instead is a mail the
45 second the exception happened. Then you get an alert and you can do
46 something about it.
47
ee5eafa Adam Zapletal Error Handling documentation fixes (grammar, etc)
adamzap authored
48 Flask uses the Python builtin logging system, and it can actually send
49 you mails for errors which is probably what you want. Here is how you can
50 configure the Flask logger to send you mails for exceptions::
ea5e654 Armin Ronacher Added a documentation chapter about logging
authored
51
52 ADMINS = ['yourname@example.com']
53 if not app.debug:
54 import logging
55 from logging.handlers import SMTPHandler
56 mail_handler = SMTPHandler('127.0.0.1',
57 'server-error@example.com',
58 ADMINS, 'YourApplication Failed')
59 mail_handler.setLevel(logging.ERROR)
60 app.logger.addHandler(mail_handler)
61
62 So what just happened? We created a new
63 :class:`~logging.handlers.SMTPHandler` that will send mails with the mail
64 server listening on ``127.0.0.1`` to all the `ADMINS` from the address
65 *server-error@example.com* with the subject "YourApplication Failed". If
ee5eafa Adam Zapletal Error Handling documentation fixes (grammar, etc)
adamzap authored
66 your mail server requires credentials, these can also be provided. For
67 that check out the documentation for the
68 :class:`~logging.handlers.SMTPHandler`.
ea5e654 Armin Ronacher Added a documentation chapter about logging
authored
69
70 We also tell the handler to only send errors and more critical messages.
71 Because we certainly don't want to get a mail for warnings or other
72 useless logs that might happen during request handling.
73
70dc2b6 Armin Ronacher Removed useless script reference. This fixes #87
authored
74 Before you run that in production, please also look at :ref:`logformat` to
75 put more information into that error mail. That will save you from a lot
76 of frustration.
ea5e654 Armin Ronacher Added a documentation chapter about logging
authored
77
78
79 Logging to a File
80 -----------------
81
82 Even if you get mails, you probably also want to log warnings. It's a
83 good idea to keep as much information around that might be required to
84 debug a problem. Please note that Flask itself will not issue any
85 warnings in the core system, so it's your responsibility to warn in the
86 code if something seems odd.
87
88 There are a couple of handlers provided by the logging system out of the
89 box but not all of them are useful for basic error logging. The most
90 interesting are probably the following:
91
38107c7 Armin Ronacher Fixed a wrong import path in the documentation. Fixes #102
authored
92 - :class:`~logging.FileHandler` - logs messages to a file on the
ea5e654 Armin Ronacher Added a documentation chapter about logging
authored
93 filesystem.
94 - :class:`~logging.handlers.RotatingFileHandler` - logs messages to a file
95 on the filesystem and will rotate after a certain number of messages.
96 - :class:`~logging.handlers.NTEventLogHandler` - will log to the system
97 event log of a Windows system. If you are deploying on a Windows box,
98 this is what you want to use.
99 - :class:`~logging.handlers.SysLogHandler` - sends logs to a UNIX
100 syslog.
101
102 Once you picked your log handler, do like you did with the SMTP handler
103 above, just make sure to use a lower setting (I would recommend
104 `WARNING`)::
105
106 if not app.debug:
107 import logging
da00160 Fixed some small typos in the documentation.
Sam Anderson authored
108 from themodule import TheHandlerYouWant
ea5e654 Armin Ronacher Added a documentation chapter about logging
authored
109 file_handler = TheHandlerYouWant(...)
110 file_handler.setLevel(logging.WARNING)
111 app.logger.addHandler(file_handler)
112
70dc2b6 Armin Ronacher Removed useless script reference. This fixes #87
authored
113 .. _logformat:
ea5e654 Armin Ronacher Added a documentation chapter about logging
authored
114
115 Controlling the Log Format
116 --------------------------
117
118 By default a handler will only write the message string into a file or
ee5eafa Adam Zapletal Error Handling documentation fixes (grammar, etc)
adamzap authored
119 send you that message as mail. A log record stores more information,
ea5e654 Armin Ronacher Added a documentation chapter about logging
authored
120 and it makes a lot of sense to configure your logger to also contain that
121 information so that you have a better idea of why that error happened, and
122 more importantly, where it did.
123
ee5eafa Adam Zapletal Error Handling documentation fixes (grammar, etc)
adamzap authored
124 A formatter can be instantiated with a format string. Note that
ea5e654 Armin Ronacher Added a documentation chapter about logging
authored
125 tracebacks are appended to the log entry automatically. You don't have to
126 do that in the log formatter format string.
127
128 Here some example setups:
129
130 Email
131 `````
132
133 ::
134
135 from logging import Formatter
136 mail_handler.setFormatter(Formatter('''
137 Message type: %(levelname)s
138 Location: %(pathname)s:%(lineno)d
139 Module: %(module)s
140 Function: %(funcName)s
141 Time: %(asctime)s
142
143 Message:
144
145 %(message)s
146 '''))
147
148 File logging
149 ````````````
150
151 ::
152
153 from logging import Formatter
154 file_handler.setFormatter(Formatter(
ea72e27 Armin Ronacher Fixed a broken example
authored
155 '%(asctime)s %(levelname)s: %(message)s '
ea5e654 Armin Ronacher Added a documentation chapter about logging
authored
156 '[in %(pathname)s:%(lineno)d]'
157 ))
158
159
160 Complex Log Formatting
161 ``````````````````````
162
163 Here is a list of useful formatting variables for the format string. Note
164 that this list is not complete, consult the official documentation of the
165 :mod:`logging` package for a full list.
166
cbcd15c Armin Ronacher Beefed up latex output. Unfortunately parts look ugly.
authored
167 .. tabularcolumns:: |p{3cm}|p{12cm}|
168
ea5e654 Armin Ronacher Added a documentation chapter about logging
authored
169 +------------------+----------------------------------------------------+
170 | Format | Description |
171 +==================+====================================================+
172 | ``%(levelname)s``| Text logging level for the message |
173 | | (``'DEBUG'``, ``'INFO'``, ``'WARNING'``, |
174 | | ``'ERROR'``, ``'CRITICAL'``). |
175 +------------------+----------------------------------------------------+
176 | ``%(pathname)s`` | Full pathname of the source file where the |
177 | | logging call was issued (if available). |
178 +------------------+----------------------------------------------------+
179 | ``%(filename)s`` | Filename portion of pathname. |
180 +------------------+----------------------------------------------------+
181 | ``%(module)s`` | Module (name portion of filename). |
182 +------------------+----------------------------------------------------+
183 | ``%(funcName)s`` | Name of function containing the logging call. |
184 +------------------+----------------------------------------------------+
185 | ``%(lineno)d`` | Source line number where the logging call was |
186 | | issued (if available). |
187 +------------------+----------------------------------------------------+
188 | ``%(asctime)s`` | Human-readable time when the LogRecord` was |
189 | | created. By default this is of the form |
190 | | ``"2003-07-08 16:49:45,896"`` (the numbers after |
191 | | the comma are millisecond portion of the time). |
192 | | This can be changed by subclassing the formatter |
193 | | and overriding the |
194 | | :meth:`~logging.Formatter.formatTime` method. |
195 +------------------+----------------------------------------------------+
196 | ``%(message)s`` | The logged message, computed as ``msg % args`` |
197 +------------------+----------------------------------------------------+
198
199 If you want to further customize the formatting, you can subclass the
200 formatter. The formatter has three interesting methods:
201
202 :meth:`~logging.Formatter.format`:
203 handles the actual formatting. It is passed a
204 :class:`~logging.LogRecord` object and has to return the formatted
205 string.
206 :meth:`~logging.Formatter.formatTime`:
207 called for `asctime` formatting. If you want a different time format
208 you can override this method.
209 :meth:`~logging.Formatter.formatException`
ee5eafa Adam Zapletal Error Handling documentation fixes (grammar, etc)
adamzap authored
210 called for exception formatting. It is passed an :attr:`~sys.exc_info`
ea5e654 Armin Ronacher Added a documentation chapter about logging
authored
211 tuple and has to return a string. The default is usually fine, you
212 don't have to override it.
213
214 For more information, head over to the official documentation.
215
216
217 Other Libraries
218 ---------------
219
220 So far we only configured the logger your application created itself.
ee5eafa Adam Zapletal Error Handling documentation fixes (grammar, etc)
adamzap authored
221 Other libraries might log themselves as well. For example, SQLAlchemy uses
222 logging heavily in its core. While there is a method to configure all
ea5e654 Armin Ronacher Added a documentation chapter about logging
authored
223 loggers at once in the :mod:`logging` package, I would not recommend using
224 it. There might be a situation in which you want to have multiple
225 separate applications running side by side in the same Python interpreter
226 and then it becomes impossible to have different logging setups for those.
227
228 Instead, I would recommend figuring out which loggers you are interested
229 in, getting the loggers with the :func:`~logging.getLogger` function and
230 iterating over them to attach handlers::
231
232 from logging import getLogger
233 loggers = [app.logger, getLogger('sqlalchemy'),
234 getLogger('otherlibrary')]
235 for logger in loggers:
236 logger.addHandler(mail_handler)
237 logger.addHandler(file_handler)
69e7a0a Ron DuPlain Move debugger details into a new section, #343.
rduplain authored
238
239
240 Debugging Application Errors
241 ============================
242
243 For production applications, configure your application with logging and
244 notifications as described in :ref:`application-errors`. This section provides
245 pointers when debugging deployment configuration and digging deeper with a
246 full-featured Python debugger.
247
248
249 When in Doubt, Run Manually
250 ---------------------------
251
252 Having problems getting your application configured for production? If you
253 have shell access to your host, verify that you can run your application
254 manually from the shell in the deployment environment. Be sure to run under
255 the same user account as the configured deployment to troubleshoot permission
256 issues. You can use Flask's builtin development server with `debug=True` on
257 your production host, which is helpful in catching configuration issues, but
258 **be sure to do this temporarily in a controlled environment.** Do not run in
259 production with `debug=True`.
260
261
262 .. _working-with-debuggers:
263
264 Working with Debuggers
265 ----------------------
266
267 To dig deeper, possibly to trace code execution, Flask provides a debugger out
268 of the box (see :ref:`debug-mode`). If you would like to use another Python
269 debugger, note that debuggers interfere with each other. You have to set some
270 options in order to use your favorite debugger:
271
272 * ``debug`` - whether to enable debug mode and catch exceptinos
273 * ``use_debugger`` - whether to use the internal Flask debugger
274 * ``use_reloader`` - whether to reload and fork the process on exception
275
276 ``debug`` must be True (i.e., exceptions must be caught) in order for the other
277 two options to have any value.
278
279 If you're using Aptana/Eclipse for debugging you'll need to set both
280 ``use_debugger`` and ``use_reloader`` to False.
281
282 A possible useful pattern for configuration is to set the following in your
fc7fe62 Andrew Ash Update docs/errorhandling.rst
ash211 authored
283 config.yaml (change the block as appropriate for your application, of course)::
69e7a0a Ron DuPlain Move debugger details into a new section, #343.
rduplain authored
284
285 FLASK:
286 DEBUG: True
287 DEBUG_WITH_APTANA: True
288
289 Then in your application's entry-point (main.py), you could have something like::
290
291 if __name__ == "__main__":
292 # To allow aptana to receive errors, set use_debugger=False
293 app = create_app(config="config.yaml")
294
295 if app.debug: use_debugger = True
296 try:
297 # Disable Flask's debugger if external debugger is requested
298 use_debugger = not(app.config.get('DEBUG_WITH_APTANA'))
299 except:
300 pass
301 app.run(use_debugger=use_debugger, debug=app.debug,
302 use_reloader=use_debugger, host='0.0.0.0')
Something went wrong with that request. Please try again.