Skip to content
Myr

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

9 files+68 added−21 removed

modified:docs/config.rst+9 added-1 removed
97 unmodified lines
98 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 so
122 unmodified lines
236.. versionadded:: 1.0237 ``SESSION_REFRESH_EACH_REQUEST``238239.. versionadded:: 1.0232 ``TEMPLATES_AUTO_RELOAD``240 ``TEMPLATES_AUTO_RELOAD``, ``LOGGER_HANDLER_POLICY``241242Configuring from Files243----------------------244
214 unmodified lines
modified:docs/upgrading.rst+10 added
17 unmodified lines
18installation, make sure to pass it the ``-U`` parameter::1920 $ easy_install -U Flask2122.. _upgrading-to-10:2324Version 1.025-----------2627Flask 1.0 removed the ``debug_log_format`` attribute from Flask28applications. Instead the new ``LOGGER_HANDLER_POLICY`` configuration can29be used to disable the default log handlers and custom log handlers can be30set up.3132.. _upgrading-to-010:3334Version 0.1035------------
347 unmodified lines
modified:flask/testsuite/basic.py+5 added-1 removed
557 unmodified lines
558559 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 lines
621 self.assert_equal(called, [1, 2, 3, 4, 5, 6])622623 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 lines
921 rv.close()922923 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 lines
991992 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 lines
modified:flask/testsuite/blueprints.py+1 added
135 unmodified lines
136137 @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', 404
680 unmodified lines
modified:flask/testsuite/helpers.py+2 added
471 unmodified lines
472473 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))479480 @app.route('/')
11 unmodified lines
492 self.assert_in('ZeroDivisionError:', err)493494 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 // 0
160 unmodified lines
modified:flask/testsuite/testing.py+1 added
134 unmodified lines
135 self.fail('Expected runtime error')136137 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 lines
modified:flask/app.py+1 added-12 removed
258 unmodified lines
259 #: 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 = True262263 #: 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 )274263 #: The JSON encoder class to use. Defaults to :class:`~flask.json.JSONEncoder`.264 #:265 #: .. versionadded:: 0.10266 json_encoder = json.JSONEncoder
17 unmodified lines
284 '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 lines
modified:flask/logging.py+36 added-7 removed
9 unmodified lines
10"""1112from __future__ import absolute_import1314from logging import getLogger, StreamHandler, Formatter, getLoggerClass, DEBUG14from logging import getLogger, StreamHandler, Formatter, getLoggerClass, \15 DEBUG, ERROR161718PROD_LOG_FORMAT = '[%(asctime)s] %(levelname)s in %(module)s: %(message)s'19DEBUG_LOG_FORMAT = (20 '-' * 80 + '\n' +21 '%(levelname)s in %(module)s [%(pathname)s:%(lineno)d]:\n' +22 '%(message)s\n' +23 '-' * 8024)252627def should_log_for(app, mode):28 policy = app.config['LOGGER_HANDLER_POLICY']29 if policy == mode or policy == 'always':30 return True31 return False323334def 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 this
8 unmodified lines
46 return DEBUG47 return Logger.getEffectiveLevel(x)4849 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)5354 class ProductionHandler(StreamHandler):55 def emit(self, record):56 if not app.debug and should_log_for(app, 'production'):57 StreamHandler.emit(self, record)5836 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))6263 prod_handler = ProductionHandler()64 prod_handler.setLevel(ERROR)65 prod_handler.setFormatter(Formatter(PROD_LOG_FORMAT))6667 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 logger
modified:CHANGES+3 added
35 unmodified lines
36 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.4344Version 0.10.245--------------46
478 unmodified lines