Django Logger: How to log Username
Asked Answered
F

1

6

I am trying to implement logging in my Django project (django 1.11, Python 3.6). I'm using default django logger.

To get the username in log, I have used django-requestlogging 1.0.1. As of now, I don't have any user other than admin superuser.

When I'm trying a GET request on front-end side, an error occurs that says 'LogSetupMiddleware' is not callable.

  1. What is the reason for this error? How do I make the logging work?

  2. How do I get AnonymousUser in the logs?

File settings.py snippet:

INSTALLED_APPS= [
    ...
    'django_requestlogging',
]

MIDDLEWARE = [
    ...
    'django_requestlogging.middleware.LogSetupMiddleware',
    ...
]

LOGGING = {
    'version': 1,
    'formatters': {
        'standard' : {
            'format': '%(asctime)s %(username)s %(request_method)s',
        }, 
    },
}
Farsighted answered 27/11, 2017 at 12:32 Comment(0)
M
1

As I can see here middleware class is really not callable. To fix it you have to override that class like this:

some/middlware/path.py

from django.utils.deprecation import MiddlewareMixin
from django_requestlogging.middleware import LogSetupMiddleware as Original

class LogSetupMiddleware(MiddlewareMixin, Original):
    pass

And replace middleware path in settings.py by new one:

some.middleware.path.LogSetupMiddleware

Mcloughlin answered 24/5, 2020 at 10:54 Comment(1)
I did it but catch error AttributeError: 'dict' object has no attribute 'iteritems'. Looks like package doesn't support Python3Heartrending

© 2022 - 2024 — McMap. All rights reserved.