I'm looking for a flexible event logging platform to store both pre-defined (username, ip address) and non-pre-defined (can be generated as needed by any piece of code) events for Django. I'm currently doing some of this with log files, but it ends up requiring various analysis scripts and ends up in a DB anyway, so I'm considering throwing it immediately into a nosql store such as MongoDB or Redis.
The idea is to be easily able to query, for example, which ip address the user most commonly comes from, whether the user has ever performed some action, lookup the outcome for a specific event, etc.
Is there something that already does this?
If not, I'm thinking of this:
The "event" is a dictionary attached to the request object. Middleware fills in various pieces (username, ip, sql timing), code fills in the rest as needed.
After the request is served a post-request hook drops the event into mongodb/redis, normalizing various fields (eg. incrementing the username:ip address counter) and dropping the rest in as is.
Words of wisdom / pointers to code that does some/all of this would be appreciated.
score:0
This question was done years ago.
However, anyone that are searching for the same issue may get some help from this answer.
Basically Django has a LogEntry
model that can handle all the user's logs from the admin app:
from django.admin.models import LogEntry
For example, you can easily use this to create an endpoint using django-rest-framework to consume the log-data, just adding this models as Meta.model = LogEntry and querysets = LogEntry.objects.all() for viewsets.
To connect with mongodb in django, today there is a greate package called djongo
.
pip install djongo
and in settings.py you can add the following code:
# database setup to connect with mongodb.
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'demodatabase',
'CLIENT': {
'host': 'your-db-host',
}
}
}
Here is one example of a log:
{
"id": 3,
"action_time": "2021-09-24T17:19:51.252000Z",
"object_id": "1",
"object_repr": "Test title",
"action_flag": 1,
"change_message": "[{\"added\": {}}]",
"user": 1,
"content_type": 7
},
You can also add more than one database, like adding only mongodb to receive logs you will just need to create routers to handle which database do what.
You can have a look at the documentation where you can get a better explanation.
score:1
I'm not sure if a library exists that does exactly what you're looking for with a NoSQL DB. However, a MongoDB backend was just released and it appears to work with Django's ORM (in a limited fashion).
Perhaps you could use this with an existing logging system that utilizes Django's built-in ORM? I hope this helps, if only a little.
score:3
The mongodb-log project can probably be used as a foundation for what you want to do.
Credit To: stackoverflow.com
Related Query
- Looking for: nosql (redis/mongodb) based event logging for Django
- Looking for Django Class based views and having multiple forms on a single page examples
- Django Class Based View for both Create and Update
- reverse for success_url on Django Class Based View complain about circular import
- Django Rest Framework: How to enable swagger docs for function based views
- Django project looking for "attribute '_session_cache'"
- Django rest framework group based permissions for individual views
- Alternatives to Django for Python based Web Development?
- Will Django be a good choice for a permissions based web-app?
- How to specify a custom 404 view for Django using Class Based Views?
- Looking for help to make npm/pdfjs-dist work with Webpack and Django
- Django - Custom permissions for function based views
- Best practices for logging in django project
- Authentication for class based views in Django
- Logging and email not working for Django for 500
- Looking for a resource which provides django templates
- Is there a plan for Django to handle NoSQL databases natively?
- Sharing a session store on Redis for a Django and a Express.js Application
- looking for django app for collaborative Wiki
- How to configure django logging for production under gunicorn server?
- reverse urls for Django class based view
- Class based default value for field in Django model inheritance hierarchy
- Key based access for the Django Rest Framework
- django built-in support for MongoDB
- How to use django class based views for sending json consist of different model querysets
- How to use redis commands for lists and sets in Django app
- Readonly View for 10 Django Class Based Views
- Django + what NOSQL is the most mature for use in production?
- Django logging format for Logstash
- How to use Redis as a Cache Server for Django project?
More Query from same tag
- pulling calculated field into template not working with DISTINCT()
- Using select_related to get related_name objects
- Python Django- How to download file on client machine in django project
- Use field label as placeholder with django-widget-tweaks
- How to check django staff user first time login in admin panel?
- Page not found (404) on django
- Django Rest Framework: pass user from view to serializer
- Specifying whitelist of domain names still allows others to authenticate with python social auth and django
- How to use csrf_token in Django RESTful API and React?
- Django-Celery in production?
- What does TypeError, __init__() missing 1 required positional argument: 'get_response' mean in python?
- Override model __init__ for an attribute which gets its value from running a method using another attribute as an argument
- Django Azure upload file to blob storage
- Filter a DJango ContentType queryset to only include models that have a specific method
- request.user.is_authenticated is always false in Django rest framework