# Path: debug_logging/utils.py
# def import_from_string(path):
#     i = path.rfind('.')
#     module, attr = path[:i], path[i + 1:]
#     try:
#         mod = import_module(module)
#     except ImportError, e:
#         raise ImproperlyConfigured(
#             'Error importing module %s: "%s"' % (module, e)
#         )
#     try:
#         instance = getattr(mod, attr)
#     except AttributeError:
#         raise ImproperlyConfigured(
#             'Module "%s" does not define a "%s" attribute' % (module, attr)
#         )
#     return instance


import re
from logging import Handler
from django.conf import settings
from django.utils.importlib import import_module
from debug_logging.utils import import_from_string


DEFAULT_CONFIG = {
    'SQL_EXTRA': False,
    'CACHE_EXTRA': False,
    'BLACKLIST': [],
    'LOGGING_HANDLERS': ('debug_logging.handlers.DBHandler',),
    'LOGGED_SETTINGS': DEFAULT_LOGGED_SETTINGS,
}

# Cache of the logging config.
_logging_config = None


def _get_logging_config():
    """
    Extend the default config with the values provided in settings.py, and then
    conduct some post-processing.
    """
    global _logging_config
    if _logging_config is None:
        _logging_config = dict(DEFAULT_CONFIG,
                               **getattr(settings, 'DEBUG_LOGGING_CONFIG', {}))

        # Instantiate the handlers
        handlers = []
        for handler in _logging_config['LOGGING_HANDLERS']:
            if isinstance(handler, Handler):
                handlers.append(handler())
            elif isinstance(handler, basestring):

# Path: debug_logging/utils.py
# def import_from_string(path):
#     i = path.rfind('.')
#     module, attr = path[:i], path[i + 1:]
#     try:
#         mod = import_module(module)
#     except ImportError, e:
#         raise ImproperlyConfigured(
#             'Error importing module %s: "%s"' % (module, e)
#         )
#     try:
#         instance = getattr(mod, attr)
#     except AttributeError:
#         raise ImproperlyConfigured(
#             'Module "%s" does not define a "%s" attribute' % (module, attr)
#         )
#     return instance


# Path: debug_logging/settings.py
import re
from logging import Handler
from django.conf import settings
from django.utils.importlib import import_module
from debug_logging.utils import import_from_string



DEFAULT_CONFIG = {
    'SQL_EXTRA': False,
    'CACHE_EXTRA': False,
    'BLACKLIST': [],
    'LOGGING_HANDLERS': ('debug_logging.handlers.DBHandler',),
    'LOGGED_SETTINGS': DEFAULT_LOGGED_SETTINGS,
}

# Cache of the logging config.
_logging_config = None


def _get_logging_config():
    """
    Extend the default config with the values provided in settings.py, and then
    conduct some post-processing.
    """
    global _logging_config
    if _logging_config is None:
        _logging_config = dict(DEFAULT_CONFIG,
                               **getattr(settings, 'DEBUG_LOGGING_CONFIG', {}))

        # Instantiate the handlers
        handlers = []
        for handler in _logging_config['LOGGING_HANDLERS']:
            if isinstance(handler, Handler):
                handlers.append(handler())
            elif isinstance(handler, basestring):

                handlers.append(import_from_string(handler)())