I am trying to implement logger functionality to my Django Application. In my App, I am having 6 different Models(from frontend users can change their Shift Timing/delete or upload new user’s records from the .xls file). I want to create a log file so that I can monitor which user is login and what action he performed(e.g update /delete/upload/download). Please find the below code I am using in my settings.py which is giving me a huge log without proper format. (However, I can see the action details in the generated log file but the structure is not user-friendly).
I am trying to implement logger functionality to my Django Application. In my App, I am having 6 different Models(from frontend users can change their Shift Timing/delete or upload new user’s records from the .xls file). I want to create a log file so that I can monitor which user is login and what action he performed(e.g update /delete/upload/download). Please find the below code I am using in my settings.py which is giving me a huge log without proper format. (However, I can see the action details in the generated log file but the structure is not user-friendly).
settings.py
```
import logging
# DataFlair #Logging Information
LOGGING = {
'version': 1,
# Version of logging
'disable_existing_loggers': False,
#########formatter#########################
#disable logging
'formatters': {
# 'console': {
# 'format': '%(name)-12s %(levelname)-8s %(message)s'
# },
'file': {
'format': '%(asctime)s %(name)-12s %(levelname)-8s %(message)s'
}
},
#########################Handlers ##################################
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': 'Apple3.log',
},
},
################################ Loggers #################################
'loggers': {
'django': {
'handlers': ['file',],
'level': 'ERROR',
'propagate': True,
'level': os.getenv('DJANGO_LOG_LEVEL', 'DEBUG')
},
},
}
```
```
```
Please find the few lines from the log file.
Traceback (most recent call last):
File "/Users/shaileshyadaav/PycharmProjects/first/venv/lib/python3.7/site-packages/django/template/base.py", line 849, in _resolve_lookup
(bit, current)) # missing attribute
django.template.base.VariableDoesNotExist: Failed lookup for key [post_list] in [{'True': True, 'False': False, 'None': None}, {'csrf_token': <SimpleLazyObject: <function csrf.<locals>._get_val at 0x10e89a170>>, 'request': <WSGIRequest: GET '/genesysall/'>, 'user': <SimpleLazyObject: <SimpleLazyObject: **<User: testuser1>>>,** 'perms': <django.contrib.auth.context_processors.PermWrapper object at 0x101aedd90>, 'messages': <django.contrib.messages.storage.fallback.FallbackStorage object at 0x10e898d10>, 'DEFAULT_MESSAGE_LEVELS': {'DEBUG': 10, 'INFO': 20, 'SUCCESS': 25, 'WARNING': 30, 'ERROR': 40}}, {}, {'allgenesys': <Page 1 of 15>, 'genesys_filter': <apple.filters.ShiftChangeFilter object at 0x10e9b9350>}, {'block': <Block Node: child__block. Contents: [<TextNode: '\n\n<style>\n span2{\n '>, <IfNode>, <TextNode: '\n\n'>]>}, {'page': <Page 1 of 15>}]
"GET /genesysall/ HTTP/1.1" 200 50073
*"GET /deletegenesys/2304183021 HTTP/1.1" 301 0*
(0.001) SELECT "django_session"."session_key", "django_session"."session_data", "django_session"."expire_date" FROM "django_session" WHERE ("django_session"."expire_date" > '2020-12-11 20:44:00.428894' AND "django_session"."session_key" = 'vvs2l3a4d3g9x3ikdfso4f0kd4py4pzv') LIMIT 21; args=('2020-12-11 20:44:00.428894', 'vvs2l3a4d3g9x3ikdfso4f0kd4py4pzv')
(0.000) SELECT "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."id" = 2 LIMIT 21; args=(2,)
(0.000) SELECT (1) AS "a" FROM "user_visit_uservisit" WHERE "user_visit_uservisit"."hash" = 'fdad239ed3sdgjsdfgjsdh4fe0b1' LIMIT 1; args=('fdad239ed33590076adshjdfgj1f84fe0b1',)
(0.002) DELETE FROM "apple_shiftchange" WHERE "apple_shiftchange"."id" IN (2304183021); args=(2304183021,)
"GET /deletegenesys/2304183021/ HTTP/1.1" 302 0
(0.001) SELECT "django_session"."session_key", "django_session"."session_data", "django_session"."expire_date" FROM "django_session" WHERE ("django_session"."expire_date" > '2020-12-11 20:44:00.440370' AND "django_session"."session_key" = 'vvs2l3a4d3g9x3ikdfso4f0kd4py4pzv') LIMIT 21; args=('2020-12-11 20:44:00.440370', 'vvs2l3asdgsdgsjdhkdfso4f0kd4py4pzv')
(0.000) SELECT "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."id" = 2 LIMIT 21; args=(2,)
(0.000) SELECT (1) AS "a" FROM "user_visit_uservisit" WHERE "user_visit_uservisit"."hash" = 'fdad239ed33hdfj651d01f84fe0b1' LIMIT 1; args=('fdad239ed33590076afdfgjdff84fe0b1',)
```
So as shown above I can see the user name who log in and what action he performed how to remove unnecessary info and format it correctly.
Thanks & Regards
Shailesh Yadav