-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Log warnings and errors to database #1050
- Loading branch information
1 parent
7309117
commit 0a92f1a
Showing
7 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Generated by Django 3.0.8 on 2020-07-20 21:16 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('dsmr_backend', '0015_backend_restart_required'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='LoggingRecord', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('created_at', models.DateTimeField(auto_now_add=True, verbose_name='Created at')), | ||
('level', models.CharField(choices=[('WARNING', 'WARNING'), ('ERROR', 'ERROR'), ('CRITICAL', 'CRITICAL')], help_text='The log level of this message', max_length=8, verbose_name='Log level')), | ||
('message', models.TextField(verbose_name='Message')), | ||
], | ||
options={ | ||
'verbose_name': 'Logging record', | ||
'verbose_name_plural': 'Logging records', | ||
'ordering': ['-pk'], | ||
'default_permissions': (), | ||
}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import logging | ||
|
||
from django.utils.translation import gettext_lazy as _ | ||
from django.db import models | ||
|
||
|
||
class LoggingRecord(models.Model): | ||
LEVEL_CHOICES = ( | ||
(logging.getLevelName(logging.WARNING), logging.getLevelName(logging.WARNING)), | ||
(logging.getLevelName(logging.ERROR), logging.getLevelName(logging.ERROR)), | ||
(logging.getLevelName(logging.CRITICAL), logging.getLevelName(logging.CRITICAL)), | ||
) | ||
|
||
created_at = models.DateTimeField( | ||
verbose_name=_('Created at'), | ||
auto_now_add=True | ||
) | ||
level = models.CharField( | ||
choices=LEVEL_CHOICES, | ||
max_length=8, | ||
verbose_name=_('Log level'), | ||
help_text=_('The log level of this message'), | ||
) | ||
message = models.TextField( | ||
verbose_name=_('Message'), | ||
) | ||
|
||
class Meta: | ||
default_permissions = tuple() | ||
ordering = ['-pk'] | ||
verbose_name = _('Logging record') | ||
verbose_name_plural = _('Logging records') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from logging import Handler | ||
|
||
|
||
class LoggingRecordHandler(Handler): | ||
""" Logs messages to the database, whenever possible. """ | ||
def emit(self, record): | ||
from dsmr_backend.models.logging import LoggingRecord | ||
|
||
LoggingRecord.objects.create( | ||
level=record.levelname, | ||
message=self.format(record) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters