How to get username in django_requestlogging

243 views
Skip to first unread message

Shailesh Yadav

unread,
Jan 2, 2021, 10:12:12 AM1/2/21
to Django users

I am using ‘django_requestlogging’ for the log file and I have followed django_requestlogging this link and configured it as per the steps given.

I am not getting the username in the log file instead of that I am getting “-”.

Please find the code details.


step1.Installed application

INSTALLED_APPS =

 [ ------- ------

  'django_requestlogging',

 ]


step2: Created Middleware


from django.utils.deprecation import MiddlewareMixin from django_requestlogging.middleware import LogSetupMiddleware as Original class LogSetupMiddleware(MiddlewareMixin, Original): pass


step3: used in settings.py


MIDDLEWARE = [ ‘django.middleware.security.SecurityMiddleware’, ‘django.contrib.sessions.middleware.SessionMiddleware’, ‘django.middleware.common.CommonMiddleware’, ‘django.middleware.csrf.CsrfViewMiddleware’, ‘django.contrib.auth.middleware.AuthenticationMiddleware’, ‘django.contrib.messages.middleware.MessageMiddleware’, ‘django.middleware.clickjacking.XFrameOptionsMiddleware’, ‘user_visit.middleware.UserVisitMiddleware’, # ‘django_requestlogging.middleware.LogSetupMiddleware’, ‘apple.middleware1.LogSetupMiddleware’ ]



step4: Configuration




LOGGING = {
'version': 1,
# Version of logging
'disable_existing_loggers': False,
'filters': {
# Add an unbound RequestFilter.
'request': {
'()': 'django_requestlogging.logging_filters.RequestFilter',
},
},
'formatters': {
'request_format': {
'format': '%(remote_addr)s "%(request_method)s '
'%(path_info)s %(server_protocol)s" %(http_user_agent)s '
'%(message)s %(asctime)s',
},

'simple': {
'format': '[%(asctime)s] - %(levelname)5s -:%(message)3s -" %(username)5s'
},

},
'handlers': {
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'filters': ['request'],
'formatter': 'simple',
},

'file': {
'level': 'INFO',
'class': 'logging.FileHandler',
'filename': 'icici.log',
'formatter':'simple'
},
},
'loggers': {
'django': {
# Add your handlers that have the unbound request filter
'handlers': ['console','file'],
'level': 'DEBUG',
'propagate': True,
# Optionally, add the unbound request filter to your
# application.
'filters': ['request'],
},
},
}

In the O/p Log file, I am getting.

[2021-01-01 21:53:39,243] - INFO -:"GET /genesysall/ HTTP/1.1" 200 82259 -" -

Any help or hint on this how to get the username.

Chetan Ganji

unread,
Jan 3, 2021, 12:39:54 PM1/3/21
to django...@googlegroups.com
Hey,

On that link it is mentioned as below.

If any of this information cannot be extracted from the current request (or there is no current request), a hyphen '-' is substituted as a placeholder.

Maybe you are trying with anonymous user? 😅




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f1f1adea-a087-42f6-b5ef-d3847b9bc43bn%40googlegroups.com.

Chetan Ganji

unread,
Jan 3, 2021, 12:42:26 PM1/3/21
to django...@googlegroups.com
However, 

use sentry for logging on prod servers, it will make your life easier.

Its free to get started 😉

Shailesh Yadav

unread,
Jan 3, 2021, 8:33:23 PM1/3/21
to Django users
Hi RyuCoder,

Thanks Actually later I have found that this module is very old (2011) and it seems that the middleware no longer works with recent versions of Django.
I am trying to implement 1st Answer by passing an extra parameter but not sure Filter where I need to define it and also it is compulsory?.

sentry => I think it is not open-source and need to take license to use in Prod env.
Reply all
Reply to author
Forward
0 new messages