Skip to content
Myr

Updated the logging documentation for recent changes.

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

authored by Armin Ronacher · committed by Armin Ronacher · parent 5edc803 · Browse files · 2 files+33 added−18 removed
This commit

State

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

Diff stats

2 files+33 added−18 removed

  • other:+33 added−18 removed

2 files+33 added−18 removed

modified:docs/errorhandling.rst+13 added-14 removed
30 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 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.35server) you might not see any log messages. The reason for that is that36Flask by default will just report to the WSGI error stream or stderr37(depending on what's available). Where this ends up is sometimes hard to38find. Often it's in your webserver's log files.3942In fact, I promise you right now that if you configure a logfile for the43application errors you will never look at it except for debugging an issue44when 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.40I can pretty much promise you however that if you only use a logfile for41the application errors you will never look at it except for debugging an42issue when a user reported it for you. What you probably want instead is43a mail the second the exception happened. Then you get an alert and you44can do something about it.4546Flask uses the Python builtin logging system, and it can actually send47you mails for errors which is probably what you want. Here is how you can48configure the Flask logger to send you mails for exceptions::
29 unmodified lines
78-----------------7980Even if you get mails, you probably also want to log warnings. It's a81good idea to keep as much information around that might be required to84debug a problem. Please note that Flask itself will not issue any85warnings in the core system, so it's your responsibility to warn in the86code if something seems odd.82debug a problem. By default as of Flask 1.0, errors are logged to your83webserver's log automatically. Warnings however are not. Please note84that Flask itself will not issue any warnings in the core system, so it's85your responsibility to warn in the code if something seems odd.8687There are a couple of handlers provided by the logging system out of the88box but not all of them are useful for basic error logging. The most89interesting are probably the following:
212 unmodified lines
modified:flask/logging.py+20 added-4 removed
9 unmodified lines
10"""1112from __future__ import absolute_import1314import sys1516from werkzeug.local import LocalProxy17from logging import getLogger, StreamHandler, Formatter, getLoggerClass, \18 DEBUG, ERROR19from .globals import _request_ctx_stack202122PROD_LOG_FORMAT = '[%(asctime)s] %(levelname)s in %(module)s: %(message)s'23DEBUG_LOG_FORMAT = (
3 unmodified lines
27 '-' * 8028)293031@LocalProxy32def _proxy_stream():33 """Finds the most appropriate error stream for the application. If a34 WSGI request is in flight we log to wsgi.errors, otherwise this resolves35 to sys.stderr.36 """37 ctx = _request_ctx_stack.top38 if ctx is not None:39 return ctx.request.environ['wsgi.errors']40 return sys.stderr414227def should_log_for(app, mode):43def _should_log_for(app, mode):44 policy = app.config['LOGGER_HANDLER_POLICY']45 if policy == mode or policy == 'always':46 return True47 return False
15 unmodified lines
63 return Logger.getEffectiveLevel(x)6465 class DebugHandler(StreamHandler):66 def emit(self, record):51 if app.debug and should_log_for(app, 'debug'):67 if app.debug and _should_log_for(app, 'debug'):68 StreamHandler.emit(self, record)6970 class ProductionHandler(StreamHandler):71 def emit(self, record):56 if not app.debug and should_log_for(app, 'production'):72 if not app.debug and _should_log_for(app, 'production'):73 StreamHandler.emit(self, record)7475 debug_handler = DebugHandler()76 debug_handler.setLevel(DEBUG)77 debug_handler.setFormatter(Formatter(DEBUG_LOG_FORMAT))7863 prod_handler = ProductionHandler()79 prod_handler = ProductionHandler(_proxy_stream)80 prod_handler.setLevel(ERROR)81 prod_handler.setFormatter(Formatter(PROD_LOG_FORMAT))8283 logger = getLogger(app.logger_name)
7 unmodified lines