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
Provenance
- Commit
7c16054- Tree
f5febc4- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
5edc803
2 files changed+33 added−18 removed
modified:docs/errorhandling.rst+13 added-14 removed
30 unmodified lines31 Error Mails32 -----------33 34 If the application runs in production mode (which it will do on your35-server) you won't see any log messages by default. Why is that? Flask36-tries to be a zero-configuration framework. Where should it drop the logs37-for you if there is no configuration? Guessing is not a good idea because38-chances are, the place it guessed is not the place where the user has39-permission to create a logfile. Also, for most small applications nobody40-will look at the logs anyways.35+server) you might not see any log messages. The reason for that is that36+Flask by default will just report to the WSGI error stream or stderr37+(depending on what's available). Where this ends up is sometimes hard to38+find. Often it's in your webserver's log files.39 42-In fact, I promise you right now that if you configure a logfile for the43-application errors you will never look at it except for debugging an issue44-when a user reported it for you. What you want instead is a mail the45-second the exception happened. Then you get an alert and you can do46-something about it.40+I can pretty much promise you however that if you only use a logfile for41+the application errors you will never look at it except for debugging an42+issue when a user reported it for you. What you probably want instead is43+a mail the second the exception happened. Then you get an alert and you44+can do something about it.45 46 Flask uses the Python builtin logging system, and it can actually send47 you mails for errors which is probably what you want. Here is how you can48 configure the Flask logger to send you mails for exceptions::29 unmodified lines78 -----------------79 80 Even if you get mails, you probably also want to log warnings. It's a81 good idea to keep as much information around that might be required to84-debug a problem. Please note that Flask itself will not issue any85-warnings in the core system, so it's your responsibility to warn in the86-code if something seems odd.82+debug a problem. By default as of Flask 1.0, errors are logged to your83+webserver's log automatically. Warnings however are not. Please note84+that Flask itself will not issue any warnings in the core system, so it's85+your responsibility to warn in the code if something seems odd.86 87 There are a couple of handlers provided by the logging system out of the88 box but not all of them are useful for basic error logging. The most89 interesting are probably the following:212 unmodified linesmodified:flask/logging.py+20 added-4 removed
9 unmodified lines10 """11 12 from __future__ import absolute_import13 14+import sys15+16+from werkzeug.local import LocalProxy17 from logging import getLogger, StreamHandler, Formatter, getLoggerClass, \18 DEBUG, ERROR19+from .globals import _request_ctx_stack20 21 22 PROD_LOG_FORMAT = '[%(asctime)s] %(levelname)s in %(module)s: %(message)s'23 DEBUG_LOG_FORMAT = (3 unmodified lines27 '-' * 8028 )29 30 31+@LocalProxy32+def _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.stderr41+42+27-def should_log_for(app, mode):43+def _should_log_for(app, mode):44 policy = app.config['LOGGER_HANDLER_POLICY']45 if policy == mode or policy == 'always':46 return True47 return False15 unmodified lines63 return Logger.getEffectiveLevel(x)64 65 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)69 70 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)74 75 debug_handler = DebugHandler()76 debug_handler.setLevel(DEBUG)77 debug_handler.setFormatter(Formatter(DEBUG_LOG_FORMAT))78 63- prod_handler = ProductionHandler()79+ prod_handler = ProductionHandler(_proxy_stream)80 prod_handler.setLevel(ERROR)81 prod_handler.setFormatter(Formatter(PROD_LOG_FORMAT))82 83 logger = getLogger(app.logger_name)7 unmodified lines