forked from orbit-oss/flask
show warning for old logger config
This commit is contained in:
parent
6665c91e6f
commit
b08e35e4ab
3 changed files with 41 additions and 3 deletions
|
|
@ -10,6 +10,7 @@ from __future__ import absolute_import
|
|||
|
||||
import logging
|
||||
import sys
|
||||
import warnings
|
||||
|
||||
from werkzeug.local import LocalProxy
|
||||
|
||||
|
|
@ -56,6 +57,20 @@ default_handler.setFormatter(
|
|||
)
|
||||
|
||||
|
||||
def _has_config(logger):
|
||||
"""Decide if a logger has direct configuration applied by checking
|
||||
its properties against the defaults.
|
||||
|
||||
:param logger: The :class:`~logging.Logger` to inspect.
|
||||
"""
|
||||
return (
|
||||
logger.level != logging.NOTSET
|
||||
or logger.handlers
|
||||
or logger.filters
|
||||
or not logger.propagate
|
||||
)
|
||||
|
||||
|
||||
def create_logger(app):
|
||||
"""Get the the Flask apps's logger and configure it if needed.
|
||||
|
||||
|
|
@ -71,7 +86,21 @@ def create_logger(app):
|
|||
"""
|
||||
logger = logging.getLogger(app.name)
|
||||
|
||||
if app.debug and logger.level == logging.NOTSET:
|
||||
# 1.1.0 changes name of logger, warn if config is detected for old
|
||||
# name and not new name
|
||||
for old_name in ("flask.app", "flask"):
|
||||
old_logger = logging.getLogger(old_name)
|
||||
|
||||
if _has_config(old_logger) and not _has_config(logger):
|
||||
warnings.warn(
|
||||
"'app.logger' is named '{name}' for this application,"
|
||||
" but configuration was found for '{old_name}', which"
|
||||
" no longer has an effect. The logging configuration"
|
||||
" should be moved to '{name}'.".format(name=app.name, old_name=old_name)
|
||||
)
|
||||
break
|
||||
|
||||
if app.debug and not logger.level:
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
if not has_level_handler(logger):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue