Saturday, January 2, 2021

How to get username in django_requestlogging

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.

--
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+unsubscribe@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.

No comments:

Post a Comment