Skip to content

Commit d36eeb5

Browse files
author
piexlMax(奇淼
committed
feat(错误日志): 实现前后端错误日志收集与管理功能
1 parent 5be3293 commit d36eeb5

File tree

15 files changed

+822
-51
lines changed

15 files changed

+822
-51
lines changed

server/api/v1/system/enter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type ApiGroup struct {
2323
AutoCodeTemplateApi
2424
SysParamsApi
2525
SysVersionApi
26+
SysErrorApi
2627
}
2728

2829
var (
@@ -46,4 +47,5 @@ var (
4647
autoCodeHistoryService = service.ServiceGroupApp.SystemServiceGroup.AutoCodeHistory
4748
autoCodeTemplateService = service.ServiceGroupApp.SystemServiceGroup.AutoCodeTemplate
4849
sysVersionService = service.ServiceGroupApp.SystemServiceGroup.SysVersionService
50+
sysErrprService = service.ServiceGroupApp.SystemServiceGroup.SysErrorService
4951
)

server/api/v1/system/sys_error.go

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package system
2+
3+
import (
4+
"github.com/flipped-aurora/gin-vue-admin/server/global"
5+
"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
6+
"github.com/flipped-aurora/gin-vue-admin/server/model/system"
7+
systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
8+
"github.com/gin-gonic/gin"
9+
"go.uber.org/zap"
10+
)
11+
12+
type SysErrorApi struct{}
13+
14+
// CreateSysError 创建错误日志
15+
// @Tags SysError
16+
// @Summary 创建错误日志
17+
// @Security ApiKeyAuth
18+
// @Accept application/json
19+
// @Produce application/json
20+
// @Param data body system.SysError true "创建错误日志"
21+
// @Success 200 {object} response.Response{msg=string} "创建成功"
22+
// @Router /sysErrpr/createSysError [post]
23+
func (sysErrprApi *SysErrorApi) CreateSysError(c *gin.Context) {
24+
// 创建业务用Context
25+
ctx := c.Request.Context()
26+
27+
var sysErrpr system.SysError
28+
err := c.ShouldBindJSON(&sysErrpr)
29+
if err != nil {
30+
response.FailWithMessage(err.Error(), c)
31+
return
32+
}
33+
err = sysErrprService.CreateSysError(ctx, &sysErrpr)
34+
if err != nil {
35+
global.GVA_LOG.Error("创建失败!", zap.Error(err))
36+
response.FailWithMessage("创建失败:"+err.Error(), c)
37+
return
38+
}
39+
response.OkWithMessage("创建成功", c)
40+
}
41+
42+
// DeleteSysError 删除错误日志
43+
// @Tags SysError
44+
// @Summary 删除错误日志
45+
// @Security ApiKeyAuth
46+
// @Accept application/json
47+
// @Produce application/json
48+
// @Param data body system.SysError true "删除错误日志"
49+
// @Success 200 {object} response.Response{msg=string} "删除成功"
50+
// @Router /sysErrpr/deleteSysError [delete]
51+
func (sysErrprApi *SysErrorApi) DeleteSysError(c *gin.Context) {
52+
// 创建业务用Context
53+
ctx := c.Request.Context()
54+
55+
ID := c.Query("ID")
56+
err := sysErrprService.DeleteSysError(ctx, ID)
57+
if err != nil {
58+
global.GVA_LOG.Error("删除失败!", zap.Error(err))
59+
response.FailWithMessage("删除失败:"+err.Error(), c)
60+
return
61+
}
62+
response.OkWithMessage("删除成功", c)
63+
}
64+
65+
// DeleteSysErrorByIds 批量删除错误日志
66+
// @Tags SysError
67+
// @Summary 批量删除错误日志
68+
// @Security ApiKeyAuth
69+
// @Accept application/json
70+
// @Produce application/json
71+
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
72+
// @Router /sysErrpr/deleteSysErrorByIds [delete]
73+
func (sysErrprApi *SysErrorApi) DeleteSysErrorByIds(c *gin.Context) {
74+
// 创建业务用Context
75+
ctx := c.Request.Context()
76+
77+
IDs := c.QueryArray("IDs[]")
78+
err := sysErrprService.DeleteSysErrorByIds(ctx, IDs)
79+
if err != nil {
80+
global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
81+
response.FailWithMessage("批量删除失败:"+err.Error(), c)
82+
return
83+
}
84+
response.OkWithMessage("批量删除成功", c)
85+
}
86+
87+
// UpdateSysError 更新错误日志
88+
// @Tags SysError
89+
// @Summary 更新错误日志
90+
// @Security ApiKeyAuth
91+
// @Accept application/json
92+
// @Produce application/json
93+
// @Param data body system.SysError true "更新错误日志"
94+
// @Success 200 {object} response.Response{msg=string} "更新成功"
95+
// @Router /sysErrpr/updateSysError [put]
96+
func (sysErrprApi *SysErrorApi) UpdateSysError(c *gin.Context) {
97+
// 从ctx获取标准context进行业务行为
98+
ctx := c.Request.Context()
99+
100+
var sysErrpr system.SysError
101+
err := c.ShouldBindJSON(&sysErrpr)
102+
if err != nil {
103+
response.FailWithMessage(err.Error(), c)
104+
return
105+
}
106+
err = sysErrprService.UpdateSysError(ctx, sysErrpr)
107+
if err != nil {
108+
global.GVA_LOG.Error("更新失败!", zap.Error(err))
109+
response.FailWithMessage("更新失败:"+err.Error(), c)
110+
return
111+
}
112+
response.OkWithMessage("更新成功", c)
113+
}
114+
115+
// FindSysError 用id查询错误日志
116+
// @Tags SysError
117+
// @Summary 用id查询错误日志
118+
// @Security ApiKeyAuth
119+
// @Accept application/json
120+
// @Produce application/json
121+
// @Param ID query uint true "用id查询错误日志"
122+
// @Success 200 {object} response.Response{data=system.SysError,msg=string} "查询成功"
123+
// @Router /sysErrpr/findSysError [get]
124+
func (sysErrprApi *SysErrorApi) FindSysError(c *gin.Context) {
125+
// 创建业务用Context
126+
ctx := c.Request.Context()
127+
128+
ID := c.Query("ID")
129+
resysErrpr, err := sysErrprService.GetSysError(ctx, ID)
130+
if err != nil {
131+
global.GVA_LOG.Error("查询失败!", zap.Error(err))
132+
response.FailWithMessage("查询失败:"+err.Error(), c)
133+
return
134+
}
135+
response.OkWithData(resysErrpr, c)
136+
}
137+
138+
// GetSysErrorList 分页获取错误日志列表
139+
// @Tags SysError
140+
// @Summary 分页获取错误日志列表
141+
// @Security ApiKeyAuth
142+
// @Accept application/json
143+
// @Produce application/json
144+
// @Param data query systemReq.SysErrorSearch true "分页获取错误日志列表"
145+
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
146+
// @Router /sysErrpr/getSysErrorList [get]
147+
func (sysErrprApi *SysErrorApi) GetSysErrorList(c *gin.Context) {
148+
// 创建业务用Context
149+
ctx := c.Request.Context()
150+
151+
var pageInfo systemReq.SysErrorSearch
152+
err := c.ShouldBindQuery(&pageInfo)
153+
if err != nil {
154+
response.FailWithMessage(err.Error(), c)
155+
return
156+
}
157+
list, total, err := sysErrprService.GetSysErrorInfoList(ctx, pageInfo)
158+
if err != nil {
159+
global.GVA_LOG.Error("获取失败!", zap.Error(err))
160+
response.FailWithMessage("获取失败:"+err.Error(), c)
161+
return
162+
}
163+
response.OkWithDetailed(response.PageResult{
164+
List: list,
165+
Total: total,
166+
Page: pageInfo.Page,
167+
PageSize: pageInfo.PageSize,
168+
}, "获取成功", c)
169+
}

server/initialize/gorm.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func RegisterTables() {
6262
system.JoinTemplate{},
6363
system.SysParams{},
6464
system.SysVersion{},
65+
system.SysError{},
6566

6667
example.ExaFile{},
6768
example.ExaCustomer{},

server/initialize/router.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func Routers() *gin.Engine {
107107
systemRouter.InitAuthorityBtnRouterRouter(PrivateGroup) // 按钮权限管理
108108
systemRouter.InitSysExportTemplateRouter(PrivateGroup, PublicGroup) // 导出模板
109109
systemRouter.InitSysParamsRouter(PrivateGroup, PublicGroup) // 参数管理
110+
systemRouter.InitSysErrorRouter(PrivateGroup, PublicGroup) // 错误日志
110111
exampleRouter.InitCustomerRouter(PrivateGroup) // 客户路由
111112
exampleRouter.InitFileUploadAndDownloadRouter(PrivateGroup) // 文件上传下载功能路由
112113
exampleRouter.InitAttachmentCategoryRouterRouter(PrivateGroup) // 文件上传下载分类
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
package request
3+
4+
import (
5+
"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
6+
"time"
7+
)
8+
9+
type SysErrorSearch struct{
10+
CreatedAtRange []time.Time `json:"createdAtRange" form:"createdAtRange[]"`
11+
Form *string `json:"form" form:"form"`
12+
Info *string `json:"info" form:"info"`
13+
request.PageInfo
14+
}

server/model/system/sys_error.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// 自动生成模板SysError
2+
package system
3+
4+
import (
5+
"github.com/flipped-aurora/gin-vue-admin/server/global"
6+
)
7+
8+
// 错误日志 结构体 SysError
9+
type SysError struct {
10+
global.GVA_MODEL
11+
Form *string `json:"form" form:"form" gorm:"comment:错误来源;column:form;type:text;" binding:"required"` //错误来源
12+
Info *string `json:"info" form:"info" gorm:"comment:错误内容;column:info;type:text;"` //错误内容
13+
Level string `json:"level" form:"level" gorm:"comment:日志等级;column:level;"`
14+
Solution *string `json:"solution" form:"solution" gorm:"comment:解决方案;column:solution;"` //解决方案
15+
}
16+
17+
// TableName 错误日志 SysError自定义表名 sys_error
18+
func (SysError) TableName() string {
19+
return "sys_error"
20+
}

server/router/system/enter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type RouterGroup struct {
2020
SysExportTemplateRouter
2121
SysParamsRouter
2222
SysVersionRouter
23+
SysErrorRouter
2324
}
2425

2526
var (
@@ -43,4 +44,5 @@ var (
4344
autoCodeTemplateApi = api.ApiGroupApp.SystemApiGroup.AutoCodeTemplateApi
4445
exportTemplateApi = api.ApiGroupApp.SystemApiGroup.SysExportTemplateApi
4546
sysVersionApi = api.ApiGroupApp.SystemApiGroup.SysVersionApi
47+
sysErrprApi = api.ApiGroupApp.SystemApiGroup.SysErrorApi
4648
)

server/router/system/sys_error.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package system
2+
3+
import (
4+
"github.com/flipped-aurora/gin-vue-admin/server/middleware"
5+
"github.com/gin-gonic/gin"
6+
)
7+
8+
type SysErrorRouter struct{}
9+
10+
// InitSysErrorRouter 初始化 错误日志 路由信息
11+
func (s *SysErrorRouter) InitSysErrorRouter(Router *gin.RouterGroup, PublicRouter *gin.RouterGroup) {
12+
sysErrprRouter := Router.Group("sysErrpr").Use(middleware.OperationRecord())
13+
sysErrprRouterWithoutRecord := Router.Group("sysErrpr")
14+
sysErrprRouterWithoutAuth := PublicRouter.Group("sysErrpr")
15+
{
16+
sysErrprRouter.DELETE("deleteSysError", sysErrprApi.DeleteSysError) // 删除错误日志
17+
sysErrprRouter.DELETE("deleteSysErrorByIds", sysErrprApi.DeleteSysErrorByIds) // 批量删除错误日志
18+
sysErrprRouter.PUT("updateSysError", sysErrprApi.UpdateSysError) // 更新错误日志
19+
}
20+
{
21+
sysErrprRouterWithoutRecord.GET("findSysError", sysErrprApi.FindSysError) // 根据ID获取错误日志
22+
sysErrprRouterWithoutRecord.GET("getSysErrorList", sysErrprApi.GetSysErrorList) // 获取错误日志列表
23+
}
24+
{
25+
sysErrprRouterWithoutAuth.POST("createSysError", sysErrprApi.CreateSysError) // 新建错误日志
26+
}
27+
}

server/service/system/enter.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ type ServiceGroup struct {
2222
AutoCodePackage autoCodePackage
2323
AutoCodeHistory autoCodeHistory
2424
AutoCodeTemplate autoCodeTemplate
25+
SysErrorService
2526
}

server/service/system/sys_error.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package system
2+
3+
import (
4+
"context"
5+
"github.com/flipped-aurora/gin-vue-admin/server/global"
6+
"github.com/flipped-aurora/gin-vue-admin/server/model/system"
7+
systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
8+
)
9+
10+
type SysErrorService struct{}
11+
12+
// CreateSysError 创建错误日志记录
13+
// Author [yourname](https://github.com/yourname)
14+
func (sysErrprService *SysErrorService) CreateSysError(ctx context.Context, sysErrpr *system.SysError) (err error) {
15+
err = global.GVA_DB.Create(sysErrpr).Error
16+
return err
17+
}
18+
19+
// DeleteSysError 删除错误日志记录
20+
// Author [yourname](https://github.com/yourname)
21+
func (sysErrprService *SysErrorService) DeleteSysError(ctx context.Context, ID string) (err error) {
22+
err = global.GVA_DB.Delete(&system.SysError{}, "id = ?", ID).Error
23+
return err
24+
}
25+
26+
// DeleteSysErrorByIds 批量删除错误日志记录
27+
// Author [yourname](https://github.com/yourname)
28+
func (sysErrprService *SysErrorService) DeleteSysErrorByIds(ctx context.Context, IDs []string) (err error) {
29+
err = global.GVA_DB.Delete(&[]system.SysError{}, "id in ?", IDs).Error
30+
return err
31+
}
32+
33+
// UpdateSysError 更新错误日志记录
34+
// Author [yourname](https://github.com/yourname)
35+
func (sysErrprService *SysErrorService) UpdateSysError(ctx context.Context, sysErrpr system.SysError) (err error) {
36+
err = global.GVA_DB.Model(&system.SysError{}).Where("id = ?", sysErrpr.ID).Updates(&sysErrpr).Error
37+
return err
38+
}
39+
40+
// GetSysError 根据ID获取错误日志记录
41+
// Author [yourname](https://github.com/yourname)
42+
func (sysErrprService *SysErrorService) GetSysError(ctx context.Context, ID string) (sysErrpr system.SysError, err error) {
43+
err = global.GVA_DB.Where("id = ?", ID).First(&sysErrpr).Error
44+
return
45+
}
46+
47+
// GetSysErrorInfoList 分页获取错误日志记录
48+
// Author [yourname](https://github.com/yourname)
49+
func (sysErrprService *SysErrorService) GetSysErrorInfoList(ctx context.Context, info systemReq.SysErrorSearch) (list []system.SysError, total int64, err error) {
50+
limit := info.PageSize
51+
offset := info.PageSize * (info.Page - 1)
52+
// 创建db
53+
db := global.GVA_DB.Model(&system.SysError{}).Order("created desc")
54+
var sysErrprs []system.SysError
55+
// 如果有条件搜索 下方会自动创建搜索语句
56+
if len(info.CreatedAtRange) == 2 {
57+
db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
58+
}
59+
60+
if info.Form != nil && *info.Form != "" {
61+
db = db.Where("form = ?", *info.Form)
62+
}
63+
if info.Info != nil && *info.Info != "" {
64+
db = db.Where("info LIKE ?", "%"+*info.Info+"%")
65+
}
66+
err = db.Count(&total).Error
67+
if err != nil {
68+
return
69+
}
70+
71+
if limit != 0 {
72+
db = db.Limit(limit).Offset(offset)
73+
}
74+
75+
err = db.Find(&sysErrprs).Error
76+
return sysErrprs, total, err
77+
}

0 commit comments

Comments
 (0)