import logging
import sys

import structlog

LOG_FORMAT = '%(message)s'
DEFAULT_STREAM = sys.stdout


class logme(object):
    """Log requests and responses"""

    def __init__(self, level=logging.DEBUG, logger=None):
        self.level = level
        if not logger:
            self.logger = logging.getLogger()
        else:
            self.logger = logger

    def __call__(self, func):

        def wrapped(*args, **kwargs):
            self.logger.log(self.level, "Entering %s", func.__name__)
            response = func(*args, **kwargs)
            self.logger.log(self.level, "Exiting %s", func.__name__,
                            response=response)
            return response
        return wrapped


def _has_streamhandler(logger, level=None, fmt=LOG_FORMAT,
                       stream=DEFAULT_STREAM):
    """Check the named logger for an appropriate existing StreamHandler.

    This only returns True if a StreamHandler that exaclty matches
    our specification is found. If other StreamHandlers are seen,
    we assume they were added for a different purpose.
    """
    for handler in logger.handlers:
        if not isinstance(handler, logging.StreamHandler):
            continue
        if handler.stream is not stream:
            continue
        if handler.level != level:
            continue
        if not handler.formatter or handler.formatter._fmt != fmt:
            continue
        return True


def get_logger(level=logging.DEBUG, name=None, stream=DEFAULT_STREAM):
    """Configure and return a logger with structlog and stdlib."""
    wrap_dict_class = structlog.threadlocal.wrap_dict(dict)
    structlog.configure(
        processors=[
            structlog.stdlib.filter_by_level,
            structlog.stdlib.add_log_level,
            structlog.stdlib.add_logger_name,
            structlog.stdlib.PositionalArgumentsFormatter(),
            structlog.processors.TimeStamper(fmt='iso'),
            structlog.processors.StackInfoRenderer(),
            structlog.processors.format_exc_info,
            structlog.processors.JSONRenderer(sort_keys=True)
        ],
        context_class=wrap_dict_class,
        logger_factory=structlog.stdlib.LoggerFactory(),
        wrapper_class=structlog.stdlib.BoundLogger,
        cache_logger_on_first_use=True)
    log = structlog.get_logger(name)
    if not _has_streamhandler(logging.getLogger(name),
                              level=level, stream=stream):
        streamhandler = logging.StreamHandler(stream)
        streamhandler.setLevel(level)
        streamhandler.setFormatter(logging.Formatter(fmt=LOG_FORMAT))
        log.addHandler(streamhandler)
    log.setLevel(level)
    return log

@@ -93,16 +93,75 @@
 .stdout%0A
+WRAPPED_DICT_CLASS = structlog.threadlocal.wrap_dict(dict)%0A
 %0A%0Aclass 
@@ -743,16 +743,17 @@
 esponse%0A
+%0A
         
@@ -1497,201 +1497,29 @@
 def 
-get_logger(level=logging.DEBUG, name=None, stream=DEFAULT_STREAM):%0A    %22%22%22Configure and return a logger with structlog and stdlib.%22%22%22%0A    wrap_dict_class = structlog.threadlocal.wrap_dict(dict)
+_configure_logger():%0A
 %0A   
@@ -2018,23 +2018,26 @@
 ass=
-wrap_dict_class
+WRAPPED_DICT_CLASS
 ,%0A  
@@ -2187,45 +2187,445 @@
 ue)%0A
-    log = structlog.get_logger(name)%0A
+%0A%0Adef get_logger(level=logging.DEBUG, name=None, stream=DEFAULT_STREAM,%0A               clobber_root_handler=True):%0A    %22%22%22Configure and return a logger with structlog and stdlib.%22%22%22%0A    _configure_logger()%0A    log = structlog.get_logger(name)%0A    root_logger = logging.getLogger()%0A    if clobber_root_handler:%0A        for handler in root_logger.handlers:%0A            handler.setFormatter(logging.Formatter(fmt=LOG_FORMAT))%0A    else:%0A    
     
@@ -2705,16 +2705,20 @@
         
+    
 level=le
@@ -2738,32 +2738,36 @@
 tream):%0A        
+ 
+   
 streamhandler = 
@@ -2796,16 +2796,20 @@
 stream)%0A
+    
         
@@ -2838,32 +2838,36 @@
 (level)%0A        
+    
 streamhandler.se
@@ -2916,24 +2916,28 @@
 T))%0A        
+   
+ 
 log.addHandl
@@ -2954,16 +2954,17 @@
 andler)%0A
+%0A
     log.
@@ -2990,12 +2990,37 @@
  return log%0A
+%0A%0AgetLogger = get_logger%0A

Clean up the way fleece instantiates loggers