Logging Django errors & capture exceptions to the clickhouse database with daily rotation.
Project description
django-clickhouse-logger
Logging Django errors to the Clickhouse database with daily rotation.
https://github.com/Sobolev5/django-clickhouse-logger
screenshot from Dbeaver
How to use it
To install run:
pip install django-clickhouse-logger # stable version
pip install -U git+https://github.com/Sobolev5/django-clickhouse-logger.git@master # development version
Add Clickhouse logger to INSTALLED_APPS:
INSTALLED_APPS = INSTALLED_APPS + ("django_clickhouse_logger",)
Set Clickhouse logger environment variables in a settings.py:
DJANGO_CLICKHOUSE_LOGGER_HOST = "127.0.0.1"
DJANGO_CLICKHOUSE_LOGGER_PORT = 9000
DJANGO_CLICKHOUSE_LOGGER_USER = "default"
DJANGO_CLICKHOUSE_LOGGER_PASSWORD = ""
DJANGO_CLICKHOUSE_LOGGER_TTL_DAY = 1 # Log rotation (in days).
DJANGO_CLICKHOUSE_LOGGER_REQUEST_EXTRA = "session"
# Means request.session.
# Extra attribute of django.core.handlers.wsgi.WSGIRequest object for logging.
# For example you define request.company in your custom middleware
# and set DJANGO_CLICKHOUSE_LOGGER_REQUEST_EXTRA = "company" in this case.
Run Clickhouse tables creation script:
python manage.py shell --command="from django_clickhouse_logger.db import *; create_logger_table(); create_capture_exception_table();"
This script will create the database django_clickhouse_logger
with tables logger
(Django errors) and capture_exception
(Captured exceptions).
Add Clickhouse logger to your logger configuration in settings.py:
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"filters": {
"require_debug_true": {"()": "django.utils.log.RequireDebugTrue",},
"require_debug_false": {"()": "django.utils.log.RequireDebugFalse"}
},
"formatters": {
"console": {"format": "%(asctime)s - %(levelname)s - %(message)s"},
},
"handlers": {
"console": {"level": "INFO", "filters": ["require_debug_true"], "class": "logging.StreamHandler", "formatter": "console"},
"django_clickhouse_logger": {"level": "ERROR", "filters": ["require_debug_false"], "class": "django_clickhouse_logger.handlers.LoggerHandler"},
},
"loggers": {
"django": {"handlers": ["console"], "level": "INFO",},
"django.request": {"handlers": ["django_clickhouse_logger"], "level": "ERROR", 'propagate': False},
},
}
If you want to test just change filter require_debug_false
to require_debug_true
for django_clickhouse_logger
handler and raise error in any django view.
For visual interface to the clickhouse table django_clickhouse_logger.logger
i recommend to using Dbeaver.
If you want to truncate tables logger
or capture_exception
just run:
python manage.py shell --command="from django_clickhouse_logger.db import *; truncate_logger_table();"
python manage.py shell --command="from django_clickhouse_logger.db import *; truncate_capture_exception_table();"
Capture exception
To catch exceptions manually:
from django_clickhouse_logger import capture_exception
try:
print(undefined_variable)
except Exception as e:
capture_exception(e)
try:
print(undefined_variable)
except Exception as e:
capture_exception(e, "add some text here")
Note: You can integrate capture_exception
function in any python project.
Django is not necessary in this case.
Integrations
django_clickhouse_logger
is default logger for upserver
project.
Go to https://github.com/Sobolev5/upserver for further instructions.
Time tracker for developers
Use Workhours.space for your working time tracking. It is free.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file django_clickhouse_logger-0.3.2-py3-none-any.whl
.
File metadata
- Download URL: django_clickhouse_logger-0.3.2-py3-none-any.whl
- Upload date:
- Size: 12.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cfe97093dfc71d4cdab8041103cb02eeaa2b56885aa53470cfefac235f992612 |
|
MD5 | 7c24bd96af441b53610c503d4601259d |
|
BLAKE2b-256 | 9569d2a74db359be9dc5d97e4d174fbdcc7a2ea108f70578eefcb29be46d90e9 |