Package: django-clickhouse-logger
django-clickhouse-loggerLogging Django errors to the Clickhouse database with daily rotation.https://github.com/Sobolev5/django-clickhouse-loggerscreenshot from DbeaverHow to use itTo install run:pipinstalldjango-clickhouse-logger# stable versionpipinstall-Ugit+https://github.com/Sobolev5/django-clickhouse-logger.git@master# development versionAdd 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=9000DJANGO_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:pythonmanage.pyshell--command="from django_clickhouse_logger.db import *; create_logger_table(); create_capture_exception_table();"This script will create the databasedjango_clickhouse_loggerwith tableslogger(Django errors) andcapture_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 filterrequire_debug_falsetorequire_debug_truefordjango_clickhouse_loggerhandler and raise error in any django view.For visual interface to the clickhouse tabledjango_clickhouse_logger.loggeri recommend to usingDbeaver.If you want to truncate tablesloggerorcapture_exceptionjust run:pythonmanage.pyshell--command="from django_clickhouse_logger.db import *; truncate_logger_table();"pythonmanage.pyshell--command="from django_clickhouse_logger.db import *; truncate_capture_exception_table();"Capture exceptionTo catch exceptions manually:fromdjango_clickhouse_loggerimportcapture_exceptiontry:print(undefined_variable)exceptExceptionase:capture_exception(e)try:print(undefined_variable)exceptExceptionase:capture_exception(e,"add some text here")Note:You can integratecapture_exceptionfunction in any python project.
Django is not necessary in this case.Integrationsdjango_clickhouse_loggeris default logger forupserverproject.Go tohttps://github.com/Sobolev5/upserverfor further instructions.Time tracker for developersUseWorkhours.spacefor your working time tracking. It is free.