# Path: python/logging.py
# class MDKHandler(StreamHandler):
#     """Hand log messages to an MDK Session."""
# 
#     def __init__(self, mdk, get_session):
#         """
#         :param mdk: A ``mdk.MDK`` instance.
#         :param get_session: Unary callable that returns the current MDK Session, or
#             ``None``, in which case a default Session will be used.
#         """
#         StreamHandler.__init__(self)
#         self._default_session = mdk.session()
#         self._get_session = get_session
# 
#     def emit(self, record):
#         level = record.levelname
#         if level == "WARNING":
#             level = "WARN"
#         session = self._get_session()
#         if session is None:
#             session = self._default_session
#         return getattr(session, level.lower())(record.name, self.format(record))


import atexit
import traceback
from mdk import start
from .logging import MDKHandler
from flask import (
    g, request, request_started, got_request_exception, request_tearing_down,
)
        new one. It will not be started or stopped.

    :return: The ``mdk.MDK`` instance.
    """
    if mdk is None:
        app.mdk = start()
        atexit.register(app.mdk.stop)
    else:
        app.mdk = mdk
    if timeout is not None:
        app.mdk.setDefaultDeadline(timeout)
    request_started.connect(_on_request_started, app)
    got_request_exception.connect(_on_request_exception, app)
    request_tearing_down.connect(_on_request_tearing_down, app)
    return app.mdk


class MDKLoggingHandler(MDKHandler):
    """
    ``logging.Handler`` that routes logs to MDK and extracts MDK session from
    the Flask request.
    """
    def __init__(self, mdk):
        """
        :param mdk: A ``mdk.MDK`` instance.
        """
        def get_session():
            if not g:
                return None
            return getattr(g, "mdk_session", None)

# Path: python/logging.py
# class MDKHandler(StreamHandler):
#     """Hand log messages to an MDK Session."""
# 
#     def __init__(self, mdk, get_session):
#         """
#         :param mdk: A ``mdk.MDK`` instance.
#         :param get_session: Unary callable that returns the current MDK Session, or
#             ``None``, in which case a default Session will be used.
#         """
#         StreamHandler.__init__(self)
#         self._default_session = mdk.session()
#         self._get_session = get_session
# 
#     def emit(self, record):
#         level = record.levelname
#         if level == "WARNING":
#             level = "WARN"
#         session = self._get_session()
#         if session is None:
#             session = self._default_session
#         return getattr(session, level.lower())(record.name, self.format(record))


# Path: python/flask.py
import atexit
import traceback
from mdk import start
from .logging import MDKHandler
from flask import (
    g, request, request_started, got_request_exception, request_tearing_down,
)

        new one. It will not be started or stopped.

    :return: The ``mdk.MDK`` instance.
    """
    if mdk is None:
        app.mdk = start()
        atexit.register(app.mdk.stop)
    else:
        app.mdk = mdk
    if timeout is not None:
        app.mdk.setDefaultDeadline(timeout)
    request_started.connect(_on_request_started, app)
    got_request_exception.connect(_on_request_exception, app)
    request_tearing_down.connect(_on_request_tearing_down, app)
    return app.mdk


class MDKLoggingHandler(MDKHandler):
    """
    ``logging.Handler`` that routes logs to MDK and extracts MDK session from
    the Flask request.
    """
    def __init__(self, mdk):
        """
        :param mdk: A ``mdk.MDK`` instance.
        """
        def get_session():
            if not g:
                return None
            return getattr(g, "mdk_session", None)

        MDKHandler.__init__(self, mdk, get_session)