forked from orbit-oss/flask
Merge pull request #3282 from pallets/logger-name
use app.name as app.logger name
This commit is contained in:
commit
6665c91e6f
8 changed files with 40 additions and 25 deletions
|
|
@ -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`
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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 <flask.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 <flask.Flask.logger>`,
|
||||
which takes the same name as :attr:`app.name <flask.Flask.name>`. This
|
||||
logger can also be used to log your own messages.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@app.route('/login', methods=['POST'])
|
||||
def login():
|
||||
|
|
|
|||
|
|
@ -653,22 +653,26 @@ class Flask(_PackageBoundObject):
|
|||
|
||||
@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
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -57,7 +57,10 @@ default_handler.setFormatter(
|
|||
|
||||
|
||||
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
|
||||
: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)
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
||||
|
|
|
|||
|
|
@ -430,7 +430,9 @@ def test_template_loader_debugging(test_apps, monkeypatch):
|
|||
|
||||
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")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue