From df470aecb96efbcb17ca76e73196c25c38ce6d17 Mon Sep 17 00:00:00 2001 From: David Lord Date: Mon, 1 Jul 2019 14:56:18 -0700 Subject: [PATCH] use app.name as app.logger name --- CHANGES.rst | 6 ++++++ docs/config.rst | 2 +- docs/errorhandling.rst | 2 +- docs/logging.rst | 12 ++++++------ src/flask/app.py | 28 ++++++++++++++++------------ src/flask/logging.py | 7 +++++-- tests/test_logging.py | 4 ++-- tests/test_templating.py | 4 +++- 8 files changed, 40 insertions(+), 25 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 59193bf7e0..7b799c9f20 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -18,6 +18,12 @@ Unreleased - :meth:`Flask.finalize_request` is called for all unhandled exceptions even if there is no ``500`` error handler. +- :attr:`Flask.logger` takes the same name as + :attr:`Flask.name` (the value passed as + ``Flask(import_name)``. This reverts 1.0's behavior of always + logging to ``"flask.app"``, in order to support multiple apps in the + same process. This may require adjusting logging configuration. + :issue:`2866`. - :meth:`flask.RequestContext.copy` includes the current session object in the request context copy. This prevents ``session`` pointing to an out-of-date object. :issue:`2935` diff --git a/docs/config.rst b/docs/config.rst index d7f0b938c1..d4036f52d4 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -382,7 +382,7 @@ The following configuration values are used internally by Flask: .. versionchanged:: 1.0 ``LOGGER_NAME`` and ``LOGGER_HANDLER_POLICY`` were removed. See - :ref:`logging` for information about configuration. + :doc:`/logging` for information about configuration. Added :data:`ENV` to reflect the :envvar:`FLASK_ENV` environment variable. diff --git a/docs/errorhandling.rst b/docs/errorhandling.rst index 9359acf88c..2f4b7335e5 100644 --- a/docs/errorhandling.rst +++ b/docs/errorhandling.rst @@ -231,7 +231,7 @@ errors, use ``getattr`` to get access it for compatibility. Logging ------- -See :ref:`logging` for information on how to log exceptions, such as by +See :doc:`/logging` for information on how to log exceptions, such as by emailing them to admins. diff --git a/docs/logging.rst b/docs/logging.rst index 937b2ae4b0..54a6b13dd6 100644 --- a/docs/logging.rst +++ b/docs/logging.rst @@ -1,12 +1,12 @@ -.. _logging: - Logging ======= -Flask uses standard Python :mod:`logging`. All Flask-related messages are -logged under the ``'flask'`` logger namespace. -:meth:`Flask.logger ` returns the logger named -``'flask.app'``, and can be used to log messages for your application. :: +Flask uses standard Python :mod:`logging`. Messages about your Flask +application are logged with :meth:`app.logger `, +which takes the same name as :attr:`app.name `. This +logger can also be used to log your own messages. + +.. code-block:: python @app.route('/login', methods=['POST']) def login(): diff --git a/src/flask/app.py b/src/flask/app.py index cead46d70f..e596fe5701 100644 --- a/src/flask/app.py +++ b/src/flask/app.py @@ -653,22 +653,26 @@ def preserve_context_on_exception(self): @locked_cached_property def logger(self): - """The ``'flask.app'`` logger, a standard Python - :class:`~logging.Logger`. + """A standard Python :class:`~logging.Logger` for the app, with + the same name as :attr:`name`. - In debug mode, the logger's :attr:`~logging.Logger.level` will be set - to :data:`~logging.DEBUG`. + In debug mode, the logger's :attr:`~logging.Logger.level` will + be set to :data:`~logging.DEBUG`. - If there are no handlers configured, a default handler will be added. - See :ref:`logging` for more information. + If there are no handlers configured, a default handler will be + added. See :doc:`/logging` for more information. - .. versionchanged:: 1.0 + .. versionchanged:: 1.1.0 + The logger takes the same name as :attr:`name` rather than + hard-coding ``"flask.app"``. + + .. versionchanged:: 1.0.0 Behavior was simplified. The logger is always named - ``flask.app``. The level is only set during configuration, it - doesn't check ``app.debug`` each time. Only one format is used, - not different ones depending on ``app.debug``. No handlers are - removed, and a handler is only added if no handlers are already - configured. + ``"flask.app"``. The level is only set during configuration, + it doesn't check ``app.debug`` each time. Only one format is + used, not different ones depending on ``app.debug``. No + handlers are removed, and a handler is only added if no + handlers are already configured. .. versionadded:: 0.3 """ diff --git a/src/flask/logging.py b/src/flask/logging.py index dde0b8a746..d223d6338e 100644 --- a/src/flask/logging.py +++ b/src/flask/logging.py @@ -57,7 +57,10 @@ def has_level_handler(logger): def create_logger(app): - """Get the ``'flask.app'`` logger and configure it if needed. + """Get the the Flask apps's logger and configure it if needed. + + The logger name will be the same as + :attr:`app.import_name `. When :attr:`~flask.Flask.debug` is enabled, set the logger level to :data:`logging.DEBUG` if it is not set. @@ -66,7 +69,7 @@ def create_logger(app): :class:`~logging.StreamHandler` for :func:`~flask.logging.wsgi_errors_stream` with a basic format. """ - logger = logging.getLogger("flask.app") + logger = logging.getLogger(app.name) if app.debug and logger.level == logging.NOTSET: logger.setLevel(logging.DEBUG) diff --git a/tests/test_logging.py b/tests/test_logging.py index a0bd3febc3..e5a96af09c 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -23,7 +23,7 @@ def reset_logging(pytestconfig): logging.root.handlers = [] root_level = logging.root.level - logger = logging.getLogger("flask.app") + logger = logging.getLogger("flask_test") logger.handlers = [] logger.setLevel(logging.NOTSET) @@ -42,7 +42,7 @@ def reset_logging(pytestconfig): def test_logger(app): - assert app.logger.name == "flask.app" + assert app.logger.name == "flask_test" assert app.logger.level == logging.NOTSET assert app.logger.handlers == [default_handler] diff --git a/tests/test_templating.py b/tests/test_templating.py index f7a0f691a6..c4bde8b1d0 100644 --- a/tests/test_templating.py +++ b/tests/test_templating.py @@ -430,7 +430,9 @@ def handle(self, record): with app.test_client() as c: monkeypatch.setitem(app.config, "EXPLAIN_TEMPLATE_LOADING", True) - monkeypatch.setattr(logging.getLogger("flask"), "handlers", [_TestHandler()]) + monkeypatch.setattr( + logging.getLogger("blueprintapp"), "handlers", [_TestHandler()] + ) with pytest.raises(TemplateNotFound) as excinfo: c.get("/missing")