show warning for old logger config
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent 6665c91 · Browse files · 3 files·+41 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+41 added−3 removed
- tests:+9 added−0 removed
- other:+32 added−3 removed
Provenance
- Commit
b08e35e- Tree
be333dd- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
6665c91
3 files changed+41 added−3 removed
modified:src/flask/logging.py+30 added-1 removed
8 unmodified lines9 from __future__ import absolute_import10 11 import logging12 import sys13+import warnings14 15 from werkzeug.local import LocalProxy16 17 from .globals import request38 unmodified lines56 logging.Formatter("[%(asctime)s] %(levelname)s in %(module)s: %(message)s")57 )58 59 60+def _has_config(logger):61+ """Decide if a logger has direct configuration applied by checking62+ its properties against the defaults.63+64+ :param logger: The :class:`~logging.Logger` to inspect.65+ """66+ return (67+ logger.level != logging.NOTSET68+ or logger.handlers69+ or logger.filters70+ or not logger.propagate71+ )72+73+74 def create_logger(app):75 """Get the the Flask apps's logger and configure it if needed.76 77 The logger name will be the same as7 unmodified lines85 :func:`~flask.logging.wsgi_errors_stream` with a basic format.86 """87 logger = logging.getLogger(app.name)88 74- if app.debug and logger.level == logging.NOTSET:89+ # 1.1.0 changes name of logger, warn if config is detected for old90+ # name and not new name91+ for old_name in ("flask.app", "flask"):92+ old_logger = logging.getLogger(old_name)93+94+ if _has_config(old_logger) and not _has_config(logger):95+ warnings.warn(96+ "'app.logger' is named '{name}' for this application,"97+ " but configuration was found for '{old_name}', which"98+ " no longer has an effect. The logging configuration"99+ " should be moved to '{name}'.".format(name=app.name, old_name=old_name)100+ )101+ break102+103+ if app.debug and not logger.level:104 logger.setLevel(logging.DEBUG)105 106 if not has_level_handler(logger):107 logger.addHandler(default_handler)2 unmodified linesmodified:tests/test_logging.py+9 added
102 unmodified lines103 assert rv.data104 err = stream.getvalue()105 assert "Exception on / [GET]" in err106 assert "Exception: test" in err107+108+109+def test_warn_old_config(app, request):110+ old_logger = logging.getLogger("flask.app")111+ old_logger.setLevel(logging.DEBUG)112+ request.addfinalizer(lambda: old_logger.setLevel(logging.NOTSET))113+114+ with pytest.warns(UserWarning):115+ assert app.logger.getEffectiveLevel() == logging.WARNINGmodified:CHANGES.rst+2 added-2 removed
20 unmodified lines21 - :attr:`Flask.logger` takes the same name as22 :attr:`Flask.name` (the value passed as23 ``Flask(import_name)``. This reverts 1.0's behavior of always24 logging to ``"flask.app"``, in order to support multiple apps in the25- same process. This may require adjusting logging configuration.26- :issue:`2866`.25+ same process. A warning will be shown if old configuration is26+ detected that needs to be moved. :issue:`2866`27 - :meth:`flask.RequestContext.copy` includes the current session28 object in the request context copy. This prevents ``session``29 pointing to an out-of-date object. :issue:`2935`30 - Using built-in RequestContext, unprintable Unicode characters in958 unmodified lines