Skip to content
Myr

Merge pull request #3282 from pallets/logger-name

Before adoption: this commit came in with the history the project adopted, so no landing records it.

use app.name as app.logger name

authored by David Lord · committed by GitHub · parents 465da9f, df470ae · Browse files · 8 files+40 added−25 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+40 added−25 removed

  • tests:+5 added−3 removed
  • other:+35 added−22 removed

8 files+40 added−25 removed

modified:docs/config.rst+1 added-1 removed
380 unmodified lines
381 ``LOGGER_HANDLER_POLICY``, ``EXPLAIN_TEMPLATE_LOADING``382383.. versionchanged:: 1.0384 ``LOGGER_NAME`` and ``LOGGER_HANDLER_POLICY`` were removed. See385 :ref:`logging` for information about configuration.385 :doc:`/logging` for information about configuration.386387 Added :data:`ENV` to reflect the :envvar:`FLASK_ENV` environment388 variable.389
308 unmodified lines
modified:docs/errorhandling.rst+1 added-1 removed
229 unmodified lines
230231Logging232-------233234See :ref:`logging` for information on how to log exceptions, such as by234See :doc:`/logging` for information on how to log exceptions, such as by235emailing them to admins.236237238Debugging Application Errors
59 unmodified lines
modified:docs/logging.rst+6 added-6 removed
1.. _logging:21Logging2=======36Flask uses standard Python :mod:`logging`. All Flask-related messages are7logged under the ``'flask'`` logger namespace.8:meth:`Flask.logger <flask.Flask.logger>` returns the logger named9``'flask.app'``, and can be used to log messages for your application. ::4Flask uses standard Python :mod:`logging`. Messages about your Flask5application are logged with :meth:`app.logger <flask.Flask.logger>`,6which takes the same name as :attr:`app.name <flask.Flask.name>`. This7logger can also be used to log your own messages.89.. code-block:: python1011 @app.route('/login', methods=['POST'])12 def login():13 user = get_user(request.form['username'])14
166 unmodified lines
modified:src/flask/app.py+16 added-12 removed
651 unmodified lines
652 return self.debug653654 @locked_cached_property655 def logger(self):656 """The ``'flask.app'`` logger, a standard Python657 :class:`~logging.Logger`.656 """A standard Python :class:`~logging.Logger` for the app, with657 the same name as :attr:`name`.658659 In debug mode, the logger's :attr:`~logging.Logger.level` will be set660 to :data:`~logging.DEBUG`.659 In debug mode, the logger's :attr:`~logging.Logger.level` will660 be set to :data:`~logging.DEBUG`.661662 If there are no handlers configured, a default handler will be added.663 See :ref:`logging` for more information.662 If there are no handlers configured, a default handler will be663 added. See :doc:`/logging` for more information.664665 .. versionchanged:: 1.0665 .. versionchanged:: 1.1.0666 The logger takes the same name as :attr:`name` rather than667 hard-coding ``"flask.app"``.668669 .. versionchanged:: 1.0.0670 Behavior was simplified. The logger is always named667 ``flask.app``. The level is only set during configuration, it668 doesn't check ``app.debug`` each time. Only one format is used,669 not different ones depending on ``app.debug``. No handlers are670 removed, and a handler is only added if no handlers are already671 configured.671 ``"flask.app"``. The level is only set during configuration,672 it doesn't check ``app.debug`` each time. Only one format is673 used, not different ones depending on ``app.debug``. No674 handlers are removed, and a handler is only added if no675 handlers are already configured.676677 .. versionadded:: 0.3678 """679 return create_logger(self)
1787 unmodified lines
modified:src/flask/logging.py+5 added-2 removed
55 unmodified lines
56)575859def create_logger(app):60 """Get the ``'flask.app'`` logger and configure it if needed.60 """Get the the Flask apps's logger and configure it if needed.6162 The logger name will be the same as63 :attr:`app.import_name <flask.Flask.name>`.6465 When :attr:`~flask.Flask.debug` is enabled, set the logger level to66 :data:`logging.DEBUG` if it is not set.6768 If there is no handler for the logger's effective level, add a69 :class:`~logging.StreamHandler` for70 :func:`~flask.logging.wsgi_errors_stream` with a basic format.71 """69 logger = logging.getLogger("flask.app")72 logger = logging.getLogger(app.name)7374 if app.debug and logger.level == logging.NOTSET:75 logger.setLevel(logging.DEBUG)76
4 unmodified lines
modified:tests/test_logging.py+2 added-2 removed
21 unmodified lines
22 root_handlers = logging.root.handlers[:]23 logging.root.handlers = []24 root_level = logging.root.level2526 logger = logging.getLogger("flask.app")26 logger = logging.getLogger("flask_test")27 logger.handlers = []28 logger.setLevel(logging.NOTSET)2930 logging_plugin = pytestconfig.pluginmanager.unregister(name="logging-plugin")
10 unmodified lines
41 pytestconfig.pluginmanager.register(logging_plugin, "logging-plugin")424344def test_logger(app):45 assert app.logger.name == "flask.app"45 assert app.logger.name == "flask_test"46 assert app.logger.level == logging.NOTSET47 assert app.logger.handlers == [default_handler]4849
57 unmodified lines
modified:tests/test_templating.py+3 added-1 removed
428 unmodified lines
429 assert "See http://flask.pocoo.org/docs/blueprints/#templates" in text430431 with app.test_client() as c:432 monkeypatch.setitem(app.config, "EXPLAIN_TEMPLATE_LOADING", True)433 monkeypatch.setattr(logging.getLogger("flask"), "handlers", [_TestHandler()])433 monkeypatch.setattr(434 logging.getLogger("blueprintapp"), "handlers", [_TestHandler()]435 )436437 with pytest.raises(TemplateNotFound) as excinfo:438 c.get("/missing")439
14 unmodified lines
modified:CHANGES.rst+6 added
16 unmodified lines
1718 - :meth:`Flask.finalize_request` is called for all unhandled19 exceptions even if there is no ``500`` error handler.2021- :attr:`Flask.logger` takes the same name as22 :attr:`Flask.name` (the value passed as23 ``Flask(import_name)``. This reverts 1.0's behavior of always24 logging to ``"flask.app"``, in order to support multiple apps in the25 same process. This may require adjusting logging configuration.26 :issue:`2866`.27- :meth:`flask.RequestContext.copy` includes the current session28 object in the request context copy. This prevents ``session``29 pointing to an out-of-date object. :issue:`2935`30- Using built-in RequestContext, unprintable Unicode characters in
958 unmodified lines