Source

django-requestlog / requestlog / util.py

Full commit
RJ Ryan 403895f 
RJ Ryan e23b4ff 












RJ Ryan 403895f 
RJ Ryan 41f699d 


RJ Ryan 403895f 





RJ Ryan e23b4ff 











RJ Ryan 403895f 






RJ Ryan e23b4ff 














import sys
import logging

from requestlog import middleware

def enable_logging_with_request_at(request_arg=0):

    """ Decorator to enable logging on a Django request method that has a
    request parameter not appearing as argument 0."""

    def inner(f):
        def new_f(*args, **kwargs):
            request = args[request_arg]
            setattr(request, middleware.LOGGING_ENABLE_ATTR, True)
            try:
                #print e
                #import traceback
                #print traceback.format_exception(*sys.exc_info())
                return f(*args, **kwargs)
            except Exception, e:
                setattr(request,
                        middleware.LOGGING_EXCEPTION,
                        sys.exc_info())
                raise e
        if hasattr(f, 'func_name'):
            new_f.func_name = f.func_name
        return new_f
    return inner

def enable_logging(f):

    """ Decorator to enable logging on a Django request method."""

    def new_f(*args, **kwargs):
        request = args[0]
        setattr(request, middleware.LOGGING_ENABLE_ATTR, True)
        try:
            return f(*args, **kwargs)
        except Exception, e:
            setattr(request,
                    middleware.LOGGING_EXCEPTION,
                    sys.exc_info())
            raise e
    if hasattr(f, 'func_name'):
        new_f.func_name = f.func_name
    return new_f

def trace(f):

    """Decorator to add traces to a method."""

    def new_f(*args, **kwargs):
        logging.info("TRACE %s ENTER" % f.func_name)
        result = f(*args, **kwargs)
        logging.info("TRACE %s EXIT" % f.func_name)
        return result
    if hasattr(f, 'func_name'):
        new_f.func_name = f.func_name
    return new_f