use app.name as app.logger name

This commit is contained in:
David Lord 2019-07-01 14:56:18 -07:00
parent 465da9f610
commit df470aecb9
No known key found for this signature in database
GPG key ID: 7A1C87E3F5BC42A8
8 changed files with 40 additions and 25 deletions

View file

@ -18,6 +18,12 @@ Unreleased
- :meth:`Flask.finalize_request` is called for all unhandled - :meth:`Flask.finalize_request` is called for all unhandled
exceptions even if there is no ``500`` error handler. 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 - :meth:`flask.RequestContext.copy` includes the current session
object in the request context copy. This prevents ``session`` object in the request context copy. This prevents ``session``
pointing to an out-of-date object. :issue:`2935` pointing to an out-of-date object. :issue:`2935`

View file

@ -382,7 +382,7 @@ The following configuration values are used internally by Flask:
.. versionchanged:: 1.0 .. versionchanged:: 1.0
``LOGGER_NAME`` and ``LOGGER_HANDLER_POLICY`` were removed. See ``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 Added :data:`ENV` to reflect the :envvar:`FLASK_ENV` environment
variable. variable.

View file

@ -231,7 +231,7 @@ errors, use ``getattr`` to get access it for compatibility.
Logging 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. emailing them to admins.

View file

@ -1,12 +1,12 @@
.. _logging:
Logging Logging
======= =======
Flask uses standard Python :mod:`logging`. All Flask-related messages are Flask uses standard Python :mod:`logging`. Messages about your Flask
logged under the ``'flask'`` logger namespace. application are logged with :meth:`app.logger <flask.Flask.logger>`,
:meth:`Flask.logger <flask.Flask.logger>` returns the logger named which takes the same name as :attr:`app.name <flask.Flask.name>`. This
``'flask.app'``, and can be used to log messages for your application. :: logger can also be used to log your own messages.
.. code-block:: python
@app.route('/login', methods=['POST']) @app.route('/login', methods=['POST'])
def login(): def login():

View file

@ -653,22 +653,26 @@ class Flask(_PackageBoundObject):
@locked_cached_property @locked_cached_property
def logger(self): def logger(self):
"""The ``'flask.app'`` logger, a standard Python """A standard Python :class:`~logging.Logger` for the app, with
:class:`~logging.Logger`. the same name as :attr:`name`.
In debug mode, the logger's :attr:`~logging.Logger.level` will be set In debug mode, the logger's :attr:`~logging.Logger.level` will
to :data:`~logging.DEBUG`. be set to :data:`~logging.DEBUG`.
If there are no handlers configured, a default handler will be added. If there are no handlers configured, a default handler will be
See :ref:`logging` for more information. 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 Behavior was simplified. The logger is always named
``flask.app``. The level is only set during configuration, it ``"flask.app"``. The level is only set during configuration,
doesn't check ``app.debug`` each time. Only one format is used, it doesn't check ``app.debug`` each time. Only one format is
not different ones depending on ``app.debug``. No handlers are used, not different ones depending on ``app.debug``. No
removed, and a handler is only added if no handlers are already handlers are removed, and a handler is only added if no
configured. handlers are already configured.
.. versionadded:: 0.3 .. versionadded:: 0.3
""" """

View file

@ -57,7 +57,10 @@ default_handler.setFormatter(
def create_logger(app): 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 <flask.Flask.name>`.
When :attr:`~flask.Flask.debug` is enabled, set the logger level to When :attr:`~flask.Flask.debug` is enabled, set the logger level to
:data:`logging.DEBUG` if it is not set. :data:`logging.DEBUG` if it is not set.
@ -66,7 +69,7 @@ def create_logger(app):
:class:`~logging.StreamHandler` for :class:`~logging.StreamHandler` for
:func:`~flask.logging.wsgi_errors_stream` with a basic format. :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: if app.debug and logger.level == logging.NOTSET:
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)

View file

@ -23,7 +23,7 @@ def reset_logging(pytestconfig):
logging.root.handlers = [] logging.root.handlers = []
root_level = logging.root.level root_level = logging.root.level
logger = logging.getLogger("flask.app") logger = logging.getLogger("flask_test")
logger.handlers = [] logger.handlers = []
logger.setLevel(logging.NOTSET) logger.setLevel(logging.NOTSET)
@ -42,7 +42,7 @@ def reset_logging(pytestconfig):
def test_logger(app): 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.level == logging.NOTSET
assert app.logger.handlers == [default_handler] assert app.logger.handlers == [default_handler]

View file

@ -430,7 +430,9 @@ def test_template_loader_debugging(test_apps, monkeypatch):
with app.test_client() as c: with app.test_client() as c:
monkeypatch.setitem(app.config, "EXPLAIN_TEMPLATE_LOADING", True) 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: with pytest.raises(TemplateNotFound) as excinfo:
c.get("/missing") c.get("/missing")