Skip to content
This repository was archived by the owner on Oct 6, 2020. It is now read-only.

Commit 1a33dc8

Browse files
committed
list per page and enable formatter options
1 parent 413cf97 commit 1a33dc8

File tree

6 files changed

+56
-10
lines changed

6 files changed

+56
-10
lines changed

README.rst

+6
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,9 @@ Quick start
8585
except Exception as e:
8686
db_logger.exception(e)
8787
88+
89+
90+
Options
91+
-------
92+
1. DJANGO_DB_LOGGER_ADMIN_LIST_PER_PAGE: integer. list per page in admin view. default ``10``
93+
2. DJANGO_DB_LOGGER_ENABLE_FORMATTER: boolean. Using ``formatter`` options to format message.``True`` or ``False``, default ``False``

dev_env/settings.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,23 @@
4444
},
4545
},
4646
'handlers': {
47-
'db': {
47+
'db_handler': {
4848
'level': 'DEBUG',
49-
'class': 'django_db_logger.db_log_handler.DatabaseLogHandler'
49+
'class': 'django_db_logger.db_log_handler.DatabaseLogHandler',
50+
'formatter': 'verbose'
5051
}
5152
},
5253
'loggers': {
53-
'db_logger': {
54-
'handlers': ['db'],
54+
'db': {
55+
'handlers': ['db_handler'],
5556
'level': 'DEBUG'
5657
}
5758
}
5859
}
5960

61+
DJANGO_DB_LOGGER_ENABLE_FORMATTER = True
62+
DJANGO_DB_LOGGER_ADMIN_LIST_PER_PAGE = 30
63+
6064
TEMPLATES = [
6165
{
6266
'BACKEND': 'django.template.backends.django.DjangoTemplates',

django_db_logger/admin.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
from django.contrib import admin
55
from django.utils.html import format_html
66

7+
from django_db_logger.config import DJANGO_DB_LOGGER_ADMIN_LIST_PER_PAGE
78
from .models import StatusLog
89

910

1011
class StatusLogAdmin(admin.ModelAdmin):
1112
list_display = ('colored_msg', 'traceback', 'create_datetime_format')
1213
list_display_links = ('colored_msg', )
1314
list_filter = ('level', )
14-
list_per_page = 10
15+
list_per_page = DJANGO_DB_LOGGER_ADMIN_LIST_PER_PAGE
1516

1617
def colored_msg(self, instance):
1718
if instance.level in [logging.NOTSET, logging.INFO]:

django_db_logger/config.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django.conf import settings
2+
3+
MSG_STYLE_SIMPLE = 'Simple'
4+
MSG_STYLE_FULL = 'Full'
5+
6+
DJANGO_DB_LOGGER_ADMIN_LIST_PER_PAGE = getattr(settings, 'DJANGO_DB_LOGGER_ADMIN_LIST_PER_PAGE', 10)
7+
8+
DJANGO_DB_LOGGER_ENABLE_FORMATTER = getattr(settings, 'DJANGO_DB_LOGGER_ENABLE_FORMATTER', False)

django_db_logger/db_log_handler.py

+31-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import logging
2-
import traceback
2+
3+
from django_db_logger.config import DJANGO_DB_LOGGER_ENABLE_FORMATTER, MSG_STYLE_SIMPLE
4+
5+
6+
db_default_formatter = logging.Formatter()
37

48

59
class DatabaseLogHandler(logging.Handler):
@@ -9,13 +13,36 @@ def emit(self, record):
913
trace = None
1014

1115
if record.exc_info:
12-
trace = traceback.format_exc()
16+
trace = db_default_formatter.formatException(record.exc_info)
17+
18+
if DJANGO_DB_LOGGER_ENABLE_FORMATTER:
19+
msg = self.format(record)
20+
else:
21+
msg = record.getMessage()
1322

1423
kwargs = {
1524
'logger_name': record.name,
1625
'level': record.levelno,
17-
'msg': record.getMessage(),
26+
'msg': msg,
1827
'trace': trace
1928
}
2029

21-
StatusLog.objects.create(**kwargs)
30+
StatusLog.objects.create(**kwargs)
31+
32+
def format(self, record):
33+
if self.formatter:
34+
fmt = self.formatter
35+
else:
36+
fmt = db_default_formatter
37+
38+
if type(fmt) == logging.Formatter:
39+
record.message = record.getMessage()
40+
41+
if fmt.usesTime():
42+
record.asctime = fmt.formatTime(record, fmt.datefmt)
43+
44+
# ignore exception traceback and stack info
45+
46+
return fmt.formatMessage(record)
47+
else:
48+
return fmt.format(record)

test_settings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@
4747
}
4848
}
4949

50-
SECRET_KEY = 'https://ciciui.com/'
50+
SECRET_KEY = 'jfkladsjfakljr;els'

0 commit comments

Comments
 (0)