Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Logger: How to log Username

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',
        }, 
    },
}
like image 405
SamCodes Avatar asked Nov 07 '22 13:11

SamCodes


1 Answers

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

like image 192
ncopiy Avatar answered Nov 15 '22 04:11

ncopiy