Skip to content

Commit 6d0e79b

Browse files
authored
Merge pull request #3284 from pallets/logger-warning
show warning for old logger config
2 parents 6665c91 + b08e35e commit 6d0e79b

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

CHANGES.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ Unreleased
2222
:attr:`Flask.name` (the value passed as
2323
``Flask(import_name)``. This reverts 1.0's behavior of always
2424
logging to ``"flask.app"``, in order to support multiple apps in the
25-
same process. This may require adjusting logging configuration.
26-
:issue:`2866`.
25+
same process. A warning will be shown if old configuration is
26+
detected that needs to be moved. :issue:`2866`
2727
- :meth:`flask.RequestContext.copy` includes the current session
2828
object in the request context copy. This prevents ``session``
2929
pointing to an out-of-date object. :issue:`2935`

src/flask/logging.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import logging
1212
import sys
13+
import warnings
1314

1415
from werkzeug.local import LocalProxy
1516

@@ -56,6 +57,20 @@ def has_level_handler(logger):
5657
)
5758

5859

60+
def _has_config(logger):
61+
"""Decide if a logger has direct configuration applied by checking
62+
its properties against the defaults.
63+
64+
:param logger: The :class:`~logging.Logger` to inspect.
65+
"""
66+
return (
67+
logger.level != logging.NOTSET
68+
or logger.handlers
69+
or logger.filters
70+
or not logger.propagate
71+
)
72+
73+
5974
def create_logger(app):
6075
"""Get the the Flask apps's logger and configure it if needed.
6176
@@ -71,7 +86,21 @@ def create_logger(app):
7186
"""
7287
logger = logging.getLogger(app.name)
7388

74-
if app.debug and logger.level == logging.NOTSET:
89+
# 1.1.0 changes name of logger, warn if config is detected for old
90+
# name and not new name
91+
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+
break
102+
103+
if app.debug and not logger.level:
75104
logger.setLevel(logging.DEBUG)
76105

77106
if not has_level_handler(logger):

tests/test_logging.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,12 @@ def index():
104104
err = stream.getvalue()
105105
assert "Exception on / [GET]" in err
106106
assert "Exception: test" in err
107+
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.WARNING

0 commit comments

Comments
 (0)