Skip to content
Myr

Merge pull request #3284 from pallets/logger-warning

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

show warning for old logger config

authored by David Lord · committed by GitHub · parents 6665c91, b08e35e · 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

3 files+41 added−3 removed

modified:src/flask/logging.py+30 added-1 removed
8 unmodified lines
9from __future__ import absolute_import1011import logging12import sys13import warnings1415from werkzeug.local import LocalProxy1617from .globals import request
38 unmodified lines
56 logging.Formatter("[%(asctime)s] %(levelname)s in %(module)s: %(message)s")57)585960def _has_config(logger):61 """Decide if a logger has direct configuration applied by checking62 its properties against the defaults.6364 :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 )727374def create_logger(app):75 """Get the the Flask apps's logger and configure it if needed.7677 The logger name will be the same as
7 unmodified lines
85 :func:`~flask.logging.wsgi_errors_stream` with a basic format.86 """87 logger = logging.getLogger(app.name)8874 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)9394 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 break102103 if app.debug and not logger.level:104 logger.setLevel(logging.DEBUG)105106 if not has_level_handler(logger):107 logger.addHandler(default_handler)
2 unmodified lines
modified:tests/test_logging.py+9 added
102 unmodified lines
103 assert rv.data104 err = stream.getvalue()105 assert "Exception on / [GET]" in err106 assert "Exception: test" in err107108109def 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))113114 with pytest.warns(UserWarning):115 assert app.logger.getEffectiveLevel() == logging.WARNING
modified:CHANGES.rst+2 added-2 removed
20 unmodified lines
21- :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 in
958 unmodified lines