Always log now, even if debug is off.
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 0ce3db8 · Browse files · 9 files·+68 added−21 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+68 added−21 removed
- other:+68 added−21 removed
Provenance
- Commit
84ad89f- Tree
88d967e- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
0ce3db8
9 files changed+68 added−21 removed
modified:docs/config.rst+9 added-1 removed
97 unmodified lines98 Non permanent sessions are not affected99 by this.100 ``USE_X_SENDFILE`` enable/disable x-sendfile101 ``LOGGER_NAME`` the name of the logger102+``LOGGER_HANDLER_POLICY`` the policy of the default logging103+ handler. The default is ``'always'``104+ which means that the default logging105+ handler is always active. ``'debug'``106+ will only activate logging in debug107+ mode, ``'production'`` will only log in108+ production and ``'never'`` disables it109+ entirely.110 ``SERVER_NAME`` the name and port number of the server.111 Required for subdomain support (e.g.:112 ``'myapp.dev:5000'``) Note that113 localhost does not support subdomains so122 unmodified lines236 .. versionadded:: 1.0237 ``SESSION_REFRESH_EACH_REQUEST``238 239 .. versionadded:: 1.0232- ``TEMPLATES_AUTO_RELOAD``240+ ``TEMPLATES_AUTO_RELOAD``, ``LOGGER_HANDLER_POLICY``241 242 Configuring from Files243 ----------------------244 214 unmodified linesmodified:docs/upgrading.rst+10 added
17 unmodified lines18 installation, make sure to pass it the ``-U`` parameter::19 20 $ easy_install -U Flask21 22+.. _upgrading-to-10:23+24+Version 1.025+-----------26+27+Flask 1.0 removed the ``debug_log_format`` attribute from Flask28+applications. Instead the new ``LOGGER_HANDLER_POLICY`` configuration can29+be used to disable the default log handlers and custom log handlers can be30+set up.31+32 .. _upgrading-to-010:33 34 Version 0.1035 ------------347 unmodified linesmodified:flask/testsuite/basic.py+5 added-1 removed
557 unmodified lines558 559 def test_teardown_request_handler_error(self):560 called = []561 app = flask.Flask(__name__)562+ app.config['LOGGER_HANDLER_POLICY'] = 'never'563 @app.teardown_request564 def teardown_request1(exc):565 self.assert_equal(type(exc), ZeroDivisionError)566 called.append(True)54 unmodified lines621 self.assert_equal(called, [1, 2, 3, 4, 5, 6])622 623 def test_error_handling(self):624 app = flask.Flask(__name__)625+ app.config['LOGGER_HANDLER_POLICY'] = 'never'626 @app.errorhandler(404)627 def not_found(e):628 return 'not found', 404629 @app.errorhandler(500)291 unmodified lines921 rv.close()922 923 def test_none_response(self):924 app = flask.Flask(__name__)925+ app.testing = True926 @app.route('/')927 def test():928 return None929 try:61 unmodified lines991 992 def test_exception_propagation(self):993 def apprunner(configkey):994 app = flask.Flask(__name__)995+ app.config['LOGGER_HANDLER_POLICY'] = 'never'996 @app.route('/')997 def index():998 1 // 0999 c = app.test_client()1000 if config_key is not None:1001 app.config[config_key] = True1002 try:999- resp = c.get('/')1003+ c.get('/')1004 except Exception:1005 pass1006 else:1007 self.fail('expected exception')391 unmodified linesmodified:flask/testsuite/blueprints.py+1 added
135 unmodified lines136 137 @emits_module_deprecation_warning138 def test_error_handling(self):139 app = flask.Flask(__name__)140+ app.config['LOGGER_HANDLER_POLICY'] = 'never'141 admin = flask.Module(__name__, 'admin')142 @admin.app_errorhandler(404)143 def not_found(e):144 return 'not found', 404680 unmodified linesmodified:flask/testsuite/helpers.py+2 added
471 unmodified lines472 473 def test_exception_logging(self):474 out = StringIO()475 app = flask.Flask(__name__)476+ app.config['LOGGER_HANDLER_POLICY'] = 'never'477 app.logger_name = 'flask_tests/test_exception_logging'478 app.logger.addHandler(StreamHandler(out))479 480 @app.route('/')11 unmodified lines492 self.assert_in('ZeroDivisionError:', err)493 494 def test_processor_exceptions(self):495 app = flask.Flask(__name__)496+ app.config['LOGGER_HANDLER_POLICY'] = 'never'497 @app.before_request498 def before_request():499 if trigger == 'before':500 1 // 0160 unmodified linesmodified:flask/testsuite/testing.py+1 added
134 unmodified lines135 self.fail('Expected runtime error')136 137 def test_test_client_context_binding(self):138 app = flask.Flask(__name__)139+ app.config['LOGGER_HANDLER_POLICY'] = 'never'140 @app.route('/')141 def index():142 flask.g.value = 42143 return 'Hello World!'114 unmodified linesmodified:flask/app.py+1 added-12 removed
258 unmodified lines259 #: in 0.7 but will be changed to False in 0.8. With Flask 1.0 modules260 #: will be removed in favor of Blueprints261 enable_modules = True262 263- #: The logging format used for the debug logger. This is only used when264- #: the application is in debug mode, otherwise the attached logging265- #: handler does the formatting.266- #:267- #: .. versionadded:: 0.3268- debug_log_format = (269- '-' * 80 + '\n' +270- '%(levelname)s in %(module)s [%(pathname)s:%(lineno)d]:\n' +271- '%(message)s\n' +272- '-' * 80273- )274-263 #: The JSON encoder class to use. Defaults to :class:`~flask.json.JSONEncoder`.264 #:265 #: .. versionadded:: 0.10266 json_encoder = json.JSONEncoder17 unmodified lines284 'SECRET_KEY': None,285 'PERMANENT_SESSION_LIFETIME': timedelta(days=31),286 'USE_X_SENDFILE': False,287 'LOGGER_NAME': None,288+ 'LOGGER_HANDLER_POLICY': 'always',289 'SERVER_NAME': None,290 'APPLICATION_ROOT': None,291 'SESSION_COOKIE_NAME': 'session',292 'SESSION_COOKIE_DOMAIN': None,1619 unmodified linesmodified:flask/logging.py+36 added-7 removed
9 unmodified lines10 """11 12 from __future__ import absolute_import13 14-from logging import getLogger, StreamHandler, Formatter, getLoggerClass, DEBUG14+from logging import getLogger, StreamHandler, Formatter, getLoggerClass, \15+ DEBUG, ERROR16 17 18+PROD_LOG_FORMAT = '[%(asctime)s] %(levelname)s in %(module)s: %(message)s'19+DEBUG_LOG_FORMAT = (20+ '-' * 80 + '\n' +21+ '%(levelname)s in %(module)s [%(pathname)s:%(lineno)d]:\n' +22+ '%(message)s\n' +23+ '-' * 8024+)25+26+27+def should_log_for(app, mode):28+ policy = app.config['LOGGER_HANDLER_POLICY']29+ if policy == mode or policy == 'always':30+ return True31+ return False32+33+34 def create_logger(app):35 """Creates a logger for the given application. This logger works36 similar to a regular Python logger but changes the effective logging37 level based on the application's debug flag. Furthermore this8 unmodified lines46 return DEBUG47 return Logger.getEffectiveLevel(x)48 49 class DebugHandler(StreamHandler):33- def emit(x, record):34- StreamHandler.emit(x, record) if app.debug else None50+ def emit(self, record):51+ if app.debug and should_log_for(app, 'debug'):52+ StreamHandler.emit(self, record)53 54+ class ProductionHandler(StreamHandler):55+ def emit(self, record):56+ if not app.debug and should_log_for(app, 'production'):57+ StreamHandler.emit(self, record)58+36- handler = DebugHandler()37- handler.setLevel(DEBUG)38- handler.setFormatter(Formatter(app.debug_log_format))59+ debug_handler = DebugHandler()60+ debug_handler.setLevel(DEBUG)61+ debug_handler.setFormatter(Formatter(DEBUG_LOG_FORMAT))62+63+ prod_handler = ProductionHandler()64+ prod_handler.setLevel(ERROR)65+ prod_handler.setFormatter(Formatter(PROD_LOG_FORMAT))66+67 logger = getLogger(app.logger_name)68 # just in case that was not a new logger, get rid of all the handlers69 # already attached to it.70 del logger.handlers[:]71 logger.__class__ = DebugLogger44- logger.addHandler(handler)72+ logger.addHandler(debug_handler)73+ logger.addHandler(prod_handler)74 return loggermodified:CHANGES+3 added
35 unmodified lines36 for an extension author to create exceptions that will by default37 result in the HTTP error of their choosing, but may be caught with38 a custom error handler if desired.39 - Added :meth:`flask.Config.from_mapping`.40+- Flask will now log by default even if debug is disabled. The log format is41+ now hardcoded but the default log handling can be disabled through the42+ ``LOGGER_HANDLER_POLICY`` configuration key.43 44 Version 0.10.245 --------------46 478 unmodified lines