Skip to content

Commit

Permalink
Merge pull request #13 from morlandi/feature/max_records
Browse files Browse the repository at this point in the history
Optionally limit the number of logged records ...
  • Loading branch information
Natgho authored Dec 5, 2023
2 parents d266b79 + 82b1129 commit 1c4d24d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions django_db_logger/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
DJANGO_DB_LOGGER_ADMIN_LIST_PER_PAGE = getattr(settings, 'DJANGO_DB_LOGGER_ADMIN_LIST_PER_PAGE', 10)

DJANGO_DB_LOGGER_ENABLE_FORMATTER = getattr(settings, 'DJANGO_DB_LOGGER_ENABLE_FORMATTER', False)

DJANGO_DB_LOGGER_MAX_LOG_RECORDS = getattr(settings, 'DJANGO_DB_LOGGER_MAX_LOG_RECORDS', 0)
13 changes: 12 additions & 1 deletion django_db_logger/db_log_handler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

from django_db_logger.config import DJANGO_DB_LOGGER_ENABLE_FORMATTER, MSG_STYLE_SIMPLE
from django_db_logger.config import DJANGO_DB_LOGGER_MAX_LOG_RECORDS


db_default_formatter = logging.Formatter()
Expand All @@ -9,7 +10,7 @@
class DatabaseLogHandler(logging.Handler):
def emit(self, record):
from .models import StatusLog

trace = None

if record.exc_info:
Expand All @@ -28,6 +29,7 @@ def emit(self, record):
}

StatusLog.objects.create(**kwargs)
self.cleanup_status_log()

def format(self, record):
if self.formatter:
Expand All @@ -46,3 +48,12 @@ def format(self, record):
return fmt.formatMessage(record)
else:
return fmt.format(record)

def cleanup_status_log(self):
from .models import StatusLog
if DJANGO_DB_LOGGER_MAX_LOG_RECORDS > 0 and \
StatusLog.objects.count() >DJANGO_DB_LOGGER_MAX_LOG_RECORDS:
# Remove olders records
queryset = StatusLog.objects.all()[DJANGO_DB_LOGGER_MAX_LOG_RECORDS:]
for row in queryset.iterator():
row.delete()

0 comments on commit 1c4d24d

Please sign in to comment.