From 2659df19192c41819813514aac5896d7550c1e5c Mon Sep 17 00:00:00 2001 From: lihuacai Date: Sun, 17 Sep 2023 22:35:29 +0800 Subject: [PATCH] feat(system): save all log --- FasterRunner/log.py | 18 ++++++++++++++++++ FasterRunner/settings/base.py | 15 ++++++++++----- db/init.sql | 14 ++++++++++++++ system/__init__.py | 0 system/admin.py | 3 +++ system/apps.py | 5 +++++ system/migrations/0001_initial.py | 30 ++++++++++++++++++++++++++++++ system/migrations/__init__.py | 0 system/models.py | 15 +++++++++++++++ system/tests.py | 3 +++ system/views.py | 3 +++ web/.dockerignore | 8 ++++++-- 12 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 FasterRunner/log.py create mode 100644 system/__init__.py create mode 100644 system/admin.py create mode 100644 system/apps.py create mode 100644 system/migrations/0001_initial.py create mode 100644 system/migrations/__init__.py create mode 100644 system/models.py create mode 100644 system/tests.py create mode 100644 system/views.py diff --git a/FasterRunner/log.py b/FasterRunner/log.py new file mode 100644 index 00000000..f742b52c --- /dev/null +++ b/FasterRunner/log.py @@ -0,0 +1,18 @@ +# !/usr/bin/python3 +# -*- coding: utf-8 -*- + +# @Author: 花菜 +# @File: log.py +# @Time : 2023/9/17 22:25 +# @Email: lihuacai168@gmail.com + +import logging + +class DatabaseLogHandler(logging.Handler): + def emit(self, record: logging.LogRecord) -> None: + from system.models import LogRecord # 引入上面定义的LogRecord模型 + LogRecord.objects.create( + request_id=record.request_id, + level=record.levelname, + message=self.format(record), + ) diff --git a/FasterRunner/settings/base.py b/FasterRunner/settings/base.py index 01e54994..6a81b5d2 100644 --- a/FasterRunner/settings/base.py +++ b/FasterRunner/settings/base.py @@ -56,6 +56,7 @@ "django_celery_beat", "rest_framework_swagger", "drf_yasg", + "system", ] MIDDLEWARE = [ @@ -232,8 +233,7 @@ "disable_existing_loggers": True, "formatters": { "standard": { - "format": "%(levelname)-2s [%(asctime)s] [%(request_id)s] %(name)s: %(message)s", - "datefmt": "%Y-%m-%d %H:%M:%S", + "format": "%(levelname)-2s [%(asctime)s] [%(request_id)s] %(name)s [%(filename)s->%(funcName)s:%(lineno)s]: %(message)s", }, "color": { "()": "colorlog.ColoredFormatter", @@ -287,20 +287,25 @@ "formatter": "color", "filters": ["request_id"], }, + 'db': { + 'level': 'INFO', + "formatter": "standard", + 'class': 'FasterRunner.log.DatabaseLogHandler', # 指向你的自定义处理器 + }, }, "loggers": { "django": { - "handlers": ["default", "console", "error"], + "handlers": ["default", "console", "error", "db"], "level": "INFO", "propagate": True, }, "fastrunner": { - "handlers": ["default", "console", "error"], + "handlers": ["default", "console", "error", "db"], "level": "INFO", "propagate": True, }, "httprunner": { - "handlers": ["default", "console", "error"], + "handlers": ["default", "console", "error", "db"], "level": "INFO", "propagate": True, }, diff --git a/db/init.sql b/db/init.sql index 0750fa10..839e3743 100644 --- a/db/init.sql +++ b/db/init.sql @@ -1329,6 +1329,20 @@ CREATE TABLE `visit` ( ) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; +DROP TABLE IF EXISTS `log_record`; +CREATE TABLE `log_record` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `create_time` datetime(6) NOT NULL, + `update_time` datetime(6) NOT NULL, + `creator` varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `updater` varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `request_id` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `level` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL, + `message` longtext COLLATE utf8mb4_unicode_ci NOT NULL, + PRIMARY KEY (`id`), + KEY `log_record_request_id_b763ce6d` (`request_id`) +) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + -- -- Dumping data for table `visit` -- diff --git a/system/__init__.py b/system/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/system/admin.py b/system/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/system/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/system/apps.py b/system/apps.py new file mode 100644 index 00000000..5dc4d64b --- /dev/null +++ b/system/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class SystemConfig(AppConfig): + name = 'system' diff --git a/system/migrations/0001_initial.py b/system/migrations/0001_initial.py new file mode 100644 index 00000000..ff5b2d51 --- /dev/null +++ b/system/migrations/0001_initial.py @@ -0,0 +1,30 @@ +# Generated by Django 2.2.17 on 2023-09-17 22:20 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='LogRecord', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('create_time', models.DateTimeField(auto_now_add=True, verbose_name='创建时间')), + ('update_time', models.DateTimeField(auto_now=True, verbose_name='更新时间')), + ('creator', models.CharField(max_length=20, null=True, verbose_name='创建人')), + ('updater', models.CharField(max_length=20, null=True, verbose_name='更新人')), + ('request_id', models.CharField(db_index=True, max_length=100, null=True)), + ('level', models.CharField(max_length=20)), + ('message', models.TextField(db_index=True)), + ], + options={ + 'db_table': 'log_record', + }, + ), + ] diff --git a/system/migrations/__init__.py b/system/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/system/models.py b/system/models.py new file mode 100644 index 00000000..0fa10253 --- /dev/null +++ b/system/models.py @@ -0,0 +1,15 @@ +from django.db import models + +from fastuser.models import BaseTable + + +# Create your models here. + + +class LogRecord(BaseTable): + class Meta: + db_table = "log_record" + request_id = models.CharField(max_length=100, null=True, db_index=True) + level = models.CharField(max_length=20) + message = models.TextField(db_index=True) + diff --git a/system/tests.py b/system/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/system/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/system/views.py b/system/views.py new file mode 100644 index 00000000..91ea44a2 --- /dev/null +++ b/system/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/web/.dockerignore b/web/.dockerignore index 1df05e62..a8b6c910 100644 --- a/web/.dockerignore +++ b/web/.dockerignore @@ -1,4 +1,8 @@ +.git +**/*.log +**/*-log-* +**/*.tar.gz Dockerfile .dockerignore -node_modules -dist +*.md +node_modules/