Best approach for audit logging in Django.

412 views
Skip to first unread message

Sencer Hamarat

unread,
Jun 3, 2022, 6:33:37 AM6/3/22
to django...@googlegroups.com
Hi,

I've been planning to add audit logging to the project.

But, I can not decide which approach is the best;
Using signals or creating middleware?

Project needs to being log events on change of model object with these informations below:

- If a record is inserted, updated and deleted:
   - who is taking this action (ForeignKey)
   - what is the action (Choice)
   - which applications model object affected (ContentType)
   - affected object (Generic relation)
   - current data in json format
   - previous data in json format


Kind regards,
Sencer HAMARAT

Jason

unread,
Jun 3, 2022, 8:58:40 AM6/3/22
to Django users
one good source when you have a question "is there anything in django that does X?" is to go to djangopackages.org.

https://djangopackages.org/grids/g/model-audit/, for example, is a list of packages for model auditing and history

Lalit Suthar

unread,
Jun 3, 2022, 2:02:04 PM6/3/22
to django...@googlegroups.com
Hi Sencer, signals will be helpful only if you are auditing on certain models 
but if you are trying to audit a set of views then middleware will be a better choice since that will be more manageable

--
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/f5586723-4e6a-4ce6-93d9-b74cd277dc91n%40googlegroups.com.

Mukul Verma

unread,
Jun 6, 2022, 12:58:43 AM6/6/22
to Django users
i also have the question regarding this, is that i have to create auditing log file for all the activities performing in the project smallest or biggest may you please tell how to initiate this task from beginning.

Thanks
Mukul Verma

Muhammad Juwaini Abdul Rahman

unread,
Jun 6, 2022, 11:50:47 PM6/6/22
to django...@googlegroups.com
Based on my experience, this is the easiest one to start with:

https://django-auditlog.readthedocs.io/

Sencer Hamarat

unread,
Jun 9, 2022, 4:20:28 AM6/9/22
to django...@googlegroups.com
Hi Lalit, 

Thanks for your reply.

Actually, It must cover all models. Not certain ones.
I thought middleware should be a much better place for this action, but I can't imagine how to handle it.

Saygılarımla,
Sencer HAMARAT



Sencer Hamarat

unread,
Jun 9, 2022, 4:26:58 AM6/9/22
to django...@googlegroups.com
Hi, Mukul

Never use a disk file for audit logging.
Use at least a DB. On the other hand, using logging facilities or specialized tools are better options.
Jason's suggestion above might lead you to the right choice.

Kind Regards,
Sencer HAMARAT



Reply all
Reply to author
Forward
0 new messages