import logging

class DjangoLoggerAdapter(logging.LoggerAdapter):
    '''Logger adapter to augment the logging context with the current user and
       the remote ip address.'''
    def __init__(self, logger, request):
        user = request.user
        if hasattr(user, 'delegate'):
            user = '%s/%s' % (user.delegate, user)
        extra = { 'ip': request.META.get('REMOTE_ADDR', '-'), 'user': user }
        logging.LoggerAdapter.__init__(self, logger, extra)

def get_logger(request, domain='docbow.views'):
    '''Return a logger object wrapped by DjangoLoggerAdapter.'''
    return DjangoLoggerAdapter(logging.getLogger(domain), request)
