Skip to content

Commit

Permalink
Log warnings and errors to database #1050
Browse files Browse the repository at this point in the history
  • Loading branch information
dennissiemensma committed Jul 20, 2020
1 parent 7309117 commit 0a92f1a
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 2 deletions.
7 changes: 7 additions & 0 deletions dsmr_backend/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from solo.admin import SingletonModelAdmin
import django.db.models.signals

from dsmr_backend.mixins import DeletionOnlyAdminModel
from dsmr_backend.models.logging import LoggingRecord
from dsmr_backend.models.settings import BackendSettings, EmailSettings
from dsmr_backend.models.schedule import ScheduledProcess
import dsmr_backend.services.email
Expand Down Expand Up @@ -147,3 +149,8 @@ def has_add_permission(self, request, obj=None):

def has_delete_permission(self, request, obj=None):
return False


@admin.register(LoggingRecord)
class LoggingRecordAdmin(DeletionOnlyAdminModel):
list_display = ('created_at', 'level', 'message')
28 changes: 28 additions & 0 deletions dsmr_backend/migrations/0016_logging_record.py
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': (),
},
),
]
32 changes: 32 additions & 0 deletions dsmr_backend/models/logging.py
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')
12 changes: 12 additions & 0 deletions dsmr_backend/services/logging.py
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)
)
6 changes: 5 additions & 1 deletion dsmrreader/config/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,14 @@
'class': 'logging.StreamHandler',
'formatter': 'simple',
},
'log_to_db': {
'level': 'WARNING',
'class': 'dsmr_backend.services.logging.LoggingRecordHandler'
}
},
'loggers': {
'dsmrreader': {
'handlers': ['console'],
'handlers': ['console', 'log_to_db'],
'level': 'WARNING',
'propagate': True,
},
Expand Down
1 change: 1 addition & 0 deletions dsmrreader/provisioning/downgrade/v3.12.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
./manage.py migrate dsmr_consumption 0015_track_power_current
./manage.py migrate dsmr_datalogger 0022_three_month_retention
./manage.py migrate dsmr_frontend 0037_v3_12_0_release
./manage.py migrate dsmr_influxdb zero
./manage.py migrate dsmr_mindergas 0005_schedule_mindergas_export
./manage.py migrate dsmr_mqtt 0014_mqtt_telegram_defaults
./manage.py migrate dsmr_notification 0007_support_for_telegram
Expand Down
2 changes: 1 addition & 1 deletion dsmrreader/provisioning/downgrade/v4.0.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Dump for DSMR-reader v4.0
./manage.py migrate dsmr_api 0003_create_api_user
./manage.py migrate dsmr_backend 0015_backend_restart_required
./manage.py migrate dsmr_backend 0016_logging_record
./manage.py migrate dsmr_backup 0011_remove_backupsettings_latest_backup
./manage.py migrate dsmr_consumption 0015_track_power_current
./manage.py migrate dsmr_datalogger 0022_three_month_retention
Expand Down

0 comments on commit 0a92f1a

Please sign in to comment.