Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I use the no_logging decorator? #122

Open
abhinavg97 opened this issue Feb 4, 2021 · 2 comments
Open

How do I use the no_logging decorator? #122

abhinavg97 opened this issue Feb 4, 2021 · 2 comments

Comments

@abhinavg97
Copy link

abhinavg97 commented Feb 4, 2021

Hi All,

Could anyone tell me how do I use the no_logging decorator?

I am not able to retrieve the correct function name from the following piece of code:

    def _should_log_route(self, request):
        # request.urlconf may be set by middleware or application level code.
        # Use this urlconf if present or default to None.
        # https://docs.djangoproject.com/en/2.1/topics/http/urls/#how-django-processes-a-request
        # https://docs.djangoproject.com/en/2.1/ref/request-response/#attributes-set-by-middleware
        urlconf = getattr(request, "urlconf", None)

        try:
            route_match = resolve(request.path, urlconf=urlconf)
        except Resolver404:
            return False, None

        method = request.method.lower()
        view = route_match.func
        func = view
        # This is for "django rest framework"
        if hasattr(view, "cls"):
            if hasattr(view, "actions"):
                actions = view.actions
                method_name = actions.get(method)
                if method_name:
                    func = getattr(view.cls, view.actions[method], None)
            else:
                func = getattr(view.cls, method, None)
        elif hasattr(view, "view_class"):
            # This is for django class-based views
            func = getattr(view.view_class, method, None)
        no_logging = getattr(func, NO_LOGGING_ATTR, False)
        return no_logging

And my no_logging function looks like this:

def no_logging():
    def wrapper(func):
        setattr(func, NO_LOGGING_ATTR, True)
        return func
    return wrapper

Note: I removed the NO_LOGGING_MSG_ATTR as I did not need that, correspondingly I made changes and removed the because variable in the remaining functions too.

I am using DRF view classes which have a few arguments.

The func evaluates to be one of the argument and not the class itself for which reason, the getattr is not able to retrieve NO_LOGGING_ATTR.

My use case is this:

@no_logging()
class MapImageViewSet(arg1, RetrieveModelMixin, UpdateModelMixin):

The func in above should_log_route class evaluates to be <function RetrieveModelMixin.retrieve at 0x7f8986b91940>.

Can someone tell if I am using the no_logging decorator correctly or there is some error in evaluating the func in the should_log_route function?

Thanks

EDIT:

So, I figured out the class we are decorating the @no_logging func with should be a view class.
And view.cls gets us the complete class name so we can retrieve the attribute set by our no_logging can be retrieved by using:

no_logging = getattr(view.cls, NO_LOGGING_ATTR, False)

The above is true for DRF use cases.
Didn't understand why are we trying to retrieve the method and stuff.
Any explanation is greatly appreciated.

@Wissperwind
Copy link

Hey,
do you know how to decorate a function with this decorator? I tried this:

@api_view(["POST"])
@permission_classes([AllowAny])
@no_logging()
def login(request):

But there is no effect.

@ozren1983
Copy link

Following combination works for me and results in no logs being written when given URL is requested.

@no_logging(value=True, silent=True)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants