Log library with ClickHouse integration
Project description
django-easy-audit-clickhouse
Logging implementation with clickhouse integration on top of django-easy-audit==1.3.7.
This app allows you to keep track of every action taken by your users.
Quickstart
-
Prerequisites:
django==4.2 clickhouse-connect>=0.8.15 celery>=5.4.0 djangorestframework>=3.15
-
Install by running
pip install django-easy-audit-clickhouse. -
Add 'easyaudit' to your
INSTALLED_APPSlike this:INSTALLED_APPS = [ ... 'easyaudit', ]
-
Add Easy Audit's middleware to your
MIDDLEWARE(orMIDDLEWARE_CLASSES) setting like this:MIDDLEWARE = ( ... 'easyaudit.middleware.easyaudit.EasyAuditMiddleware', )
-
Run
python manage.py migrate easyauditto create the app's models. -
Configure the ClickHouse connection in your settings.py:
CLICKHOUSE_USER = 'user' CLICKHOUSE_PASSWORD = 'password' CLICKHOUSE_HOST = 'localhost' CLICKHOUSE_PORT = 8123 CLICKHOUSE_DATABASE = 'default' CLICKHOUSE_SECURE = False
-
Create shared task of
send_logs_to_clickhouseto sync data from django to clickhouse:@shared_task def send_audit_logs_to_clickhouse(): from easyaudit.tasks import send_logs_to_clickhouse send_logs_to_clickhouse() app.conf.beat_schedule = { "send-logs-to-clickhouse": { "task": "path.to.send_logs_to_clickhouse", "schedule": crontab(hour=9, minute=10), # 12:00 AM PST }, }
- Add
SEND_LOGS_TO_CLICKHOUSEto settings with True/False for enable/disable data push to clickhouse
- Add
Settings
For an exhaustive list of available settings, please check our wiki.
Below are some of the settings you may want to use. These should be defined in your project's settings.py file:
-
DJANGO_EASY_AUDIT_WATCH_MODEL_EVENTS -
DJANGO_EASY_AUDIT_WATCH_AUTH_EVENTS -
DJANGO_EASY_AUDIT_WATCH_REQUEST_EVENTSSet these to
Falseto stop logging model, authentication, and/or request events. -
DJANGO_EASY_AUDIT_UNREGISTERED_CLASSES_EXTRAA list of Django models which will be ignored by Django Easy Audit. Use it to prevent logging one or more of your project's models. List items can be classes or strings with
app_name.model_nameformat. -
DJANGO_EASY_AUDIT_UNREGISTERED_URLS_EXTRAA list of URLs which will be ignored by Django Easy Audit. List items are expected to be regular expressions that will be matched against the URL path.
-
DJANGO_EASY_AUDIT_CRUD_DIFFERENCE_CALLBACKSMay point to a list of callables/string-paths-to-functions-classes in which the application code can determine on a per CRUDEvent whether or not the application chooses to create the CRUDEvent or not. This is different from the registered/unregistered settings (e.g.
DJANGO_EASY_AUDIT_UNREGISTERED_CLASSES_EXTRA). This is meant to be for dynamic configurations where the application may inspect the current save/create/delete and choose whether or not to save that into the database or ignore it. -
DJANGO_EASY_AUDIT_USER_DB_CONSTRAINTDefault is
True. This is reserved for future use (does not do anything yet). The functionality provided by the setting (whether enabled or disabled) could be handled more explicitly in certain code paths (or even internally as custom model managers). For projects that separate the easyaudit database, such that the tables are not on the same database as the user table, this could help with making certain queries easier. Again, this doesn't do anything yet, and if it ever does, the version will be increased and the README will be updated accordingly. If you keep your database together (the standard usage), you have nothing to worry about. -
DJANGO_EASY_AUDIT_CRUD_EVENT_LIST_FILTER -
DJANGO_EASY_AUDIT_LOGIN_EVENT_LIST_FILTER -
DJANGO_EASY_AUDIT_REQUEST_EVENT_LIST_FILTERChangeview filters configuration. Used to remove filters when the corresponding list of data would be too long. Defaults are:
- ['event_type', 'user', 'created_at', ] for CRUDEventAdmin
- ['login_type', 'user', 'created_at', ] for LoginEventAdmin
-
DJANGO_EASY_AUDIT_DATABASE_ALIASBy default it is the Django
defaultdatabase alias. But for projects that have split databases, this is necessary in order to keep database atomicity concerns in check during signal handlers.To clarify, this is only truly necessary for the model signals.
-
DJANGO_EASY_AUDIT_PROPAGATE_EXCEPTIONSDefault is
False. When set toTrue, easyaudit will propagate exceptions occurred in own signal handlers. The recommended approach is to use Django'sDEBUGsetting in order to only propagate errors in development:DJANGO_EASY_AUDIT_PROPAGATE_EXCEPTIONS = DEBUG
-
DJANGO_EASY_AUDIT_CRUD_EVENT_NO_CHANGED_FIELDS_SKIPBy default this is
False, but this allows the calling project not to saveCRUDEventif the changed fields as determined by thepre_savehandler sees that there are no changed fields. We are keeping it off by default so that projects that wish to use this (potentially lessCRUDEvent) can choose to turn it on! And those that do not want it (yet or ever), or those that do not closely follow the release notes of this project will have one less worry when upgrading. -
DJANGO_EASY_AUDIT_CHECK_IF_REQUEST_USER_EXISTSBy default this is
True, but this allows the calling project to make easyaudit ignore user validation on audit event creation. This is useful when you have a app with soft delete or no delete on users model. With this set toFalse, easyaudit only fetchrequest.userfor audit event creation, no db check is made, meaning you can speed up audit events creation and save some DB calls. -
DJANGO_EASY_AUDIT_READONLY_EVENTSDefault is
False. The events visible through the admin interface are editable by default by a superuser. Set this toTrueif you wish to make the recorded events read-only through the admin UI. -
DJANGO_EASY_AUDIT_LOGGING_BACKENDA pluggable backend option for logging. Defaults to
easyaudit.backends.ModelBackend. This class expects to have 3 methods:login(self, login_info_dict):crud(self, crud_info_dict):request(self, request_info_dict):
each of these methods accept a dictionary containing the info regarding the event. example overriding:
import logging class PythonLoggerBackend: logging.basicConfig() logger = logging.getLogger('your-kibana-logger') logger.setLevel(logging.DEBUG) def request(self, request_info): return request_info # if you don't need it def login(self, login_info): self.logger.info(msg='your message', extra=login_info) return login_info def crud(self, crud_info): self.logger.info(msg='your message', extra=crud_info) return crud_info
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_easy_audit_clickhouse-2.0.3.tar.gz.
File metadata
- Download URL: django_easy_audit_clickhouse-2.0.3.tar.gz
- Upload date:
- Size: 40.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c654146998c3cb0fefa54d549719706f44042224164fa64f780ca1e18630d684
|
|
| MD5 |
a35791cc6396c3df27249de5b850b1f5
|
|
| BLAKE2b-256 |
6f4fd221d9092925941839d4fe3ad412df0ec65e916f8effcef32b4df75f9725
|
File details
Details for the file django_easy_audit_clickhouse-2.0.3-py3-none-any.whl.
File metadata
- Download URL: django_easy_audit_clickhouse-2.0.3-py3-none-any.whl
- Upload date:
- Size: 51.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dcb4005a3333ab5aa4683909fd68330041d4e6b272949989775e797e37767382
|
|
| MD5 |
b0330611742eccf52ddc279f794fba76
|
|
| BLAKE2b-256 |
66bbcfa5a67963306c63dd3890f2dec80f87db89980dfbe48635f5909da0736f
|