Home / File/ logging.py — flask Source File

logging.py — flask Source File

Architecture documentation for logging.py, a python file in the flask codebase. 6 imports, 1 dependents.

File python ApplicationCore Scaffolding 6 imports 1 dependents 4 functions

Entity Profile

Dependency Diagram

graph LR
  3bd07725_f871_e2d4_476f_62e7d3f6e96a["logging.py"]
  9cff5a62_7dbb_7b80_cf3c_128a7a2fda28["globals.py"]
  3bd07725_f871_e2d4_476f_62e7d3f6e96a --> 9cff5a62_7dbb_7b80_cf3c_128a7a2fda28
  0bdbbb6d_41ac_cb65_54a5_10ce0159c2d6["sansio.app"]
  3bd07725_f871_e2d4_476f_62e7d3f6e96a --> 0bdbbb6d_41ac_cb65_54a5_10ce0159c2d6
  3bd07725_f871_e2d4_476f_62e7d3f6e96a["logging.py"]
  3bd07725_f871_e2d4_476f_62e7d3f6e96a --> 3bd07725_f871_e2d4_476f_62e7d3f6e96a
  8e9cc1cb_fabe_237c_ff24_85400e094882["sys"]
  3bd07725_f871_e2d4_476f_62e7d3f6e96a --> 8e9cc1cb_fabe_237c_ff24_85400e094882
  d3e9218c_bf0a_48f5_15c9_90795af6f3ac["typing.py"]
  3bd07725_f871_e2d4_476f_62e7d3f6e96a --> d3e9218c_bf0a_48f5_15c9_90795af6f3ac
  5301647b_918a_536b_6b91_a88158392196["werkzeug.local"]
  3bd07725_f871_e2d4_476f_62e7d3f6e96a --> 5301647b_918a_536b_6b91_a88158392196
  3bd07725_f871_e2d4_476f_62e7d3f6e96a["logging.py"]
  3bd07725_f871_e2d4_476f_62e7d3f6e96a --> 3bd07725_f871_e2d4_476f_62e7d3f6e96a
  style 3bd07725_f871_e2d4_476f_62e7d3f6e96a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from __future__ import annotations

import logging
import sys
import typing as t

from werkzeug.local import LocalProxy

from .globals import request

if t.TYPE_CHECKING:  # pragma: no cover
    from .sansio.app import App


@LocalProxy
def wsgi_errors_stream() -> t.TextIO:
    """Find the most appropriate error stream for the application. If a request
    is active, log to ``wsgi.errors``, otherwise use ``sys.stderr``.

    If you configure your own :class:`logging.StreamHandler`, you may want to
    use this for the stream. If you are using file or dict configuration and
    can't import this directly, you can refer to it as
    ``ext://flask.logging.wsgi_errors_stream``.
    """
    if request:
        return request.environ["wsgi.errors"]  # type: ignore[no-any-return]

    return sys.stderr


def has_level_handler(logger: logging.Logger) -> bool:
    """Check if there is a handler in the logging chain that will handle the
    given logger's :meth:`effective level <~logging.Logger.getEffectiveLevel>`.
    """
    level = logger.getEffectiveLevel()
    current = logger

    while current:
        if any(handler.level <= level for handler in current.handlers):
            return True

        if not current.propagate:
            break

        current = current.parent  # type: ignore

    return False


#: Log messages to :func:`~flask.logging.wsgi_errors_stream` with the format
#: ``[%(asctime)s] %(levelname)s in %(module)s: %(message)s``.
default_handler = logging.StreamHandler(wsgi_errors_stream)  # type: ignore
default_handler.setFormatter(
    logging.Formatter("[%(asctime)s] %(levelname)s in %(module)s: %(message)s")
)


def create_logger(app: App) -> logging.Logger:
    """Get the Flask app'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.

    If there is no handler for the logger's effective level, add a
    :class:`~logging.StreamHandler` for
    :func:`~flask.logging.wsgi_errors_stream` with a basic format.
    """
    logger = logging.getLogger(app.name)

    if app.debug and not logger.level:
        logger.setLevel(logging.DEBUG)

    if not has_level_handler(logger):
        logger.addHandler(default_handler)

    return logger

Subdomains

Dependencies

Imported By

Frequently Asked Questions

What does logging.py do?
logging.py is a source file in the flask codebase, written in python. It belongs to the ApplicationCore domain, Scaffolding subdomain.
What functions are defined in logging.py?
logging.py defines 4 function(s): create_logger, has_level_handler, sansio, wsgi_errors_stream.
What does logging.py depend on?
logging.py imports 6 module(s): globals.py, logging.py, sansio.app, sys, typing.py, werkzeug.local.
What files import logging.py?
logging.py is imported by 1 file(s): logging.py.
Where is logging.py in the architecture?
logging.py is located at src/flask/logging.py (domain: ApplicationCore, subdomain: Scaffolding, directory: src/flask).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free