Skip to content

Commit

Permalink
Merge pull request #111 from lihuacai168/feat/save-log
Browse files Browse the repository at this point in the history
feat(system): save all log
  • Loading branch information
lihuacai168 committed Sep 17, 2023
2 parents c110d14 + 2659df1 commit d867447
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 7 deletions.
18 changes: 18 additions & 0 deletions FasterRunner/log.py
Original file line number Diff line number Diff line change
@@ -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),
)
15 changes: 10 additions & 5 deletions FasterRunner/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"django_celery_beat",
"rest_framework_swagger",
"drf_yasg",
"system",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
},
Expand Down
14 changes: 14 additions & 0 deletions db/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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`
--
Expand Down
Empty file added system/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions system/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions system/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class SystemConfig(AppConfig):
name = 'system'
30 changes: 30 additions & 0 deletions system/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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',
},
),
]
Empty file added system/migrations/__init__.py
Empty file.
15 changes: 15 additions & 0 deletions system/models.py
Original file line number Diff line number Diff line change
@@ -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)

3 changes: 3 additions & 0 deletions system/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions system/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
8 changes: 6 additions & 2 deletions web/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
.git
**/*.log
**/*-log-*
**/*.tar.gz
Dockerfile
.dockerignore
node_modules
dist
*.md
node_modules/

0 comments on commit d867447

Please sign in to comment.