Skip to content

Commit 7a80b2b

Browse files
增加日志请求头和日志清空功能 (opsre#342)
* feat: 增加操作日志请求头查询和清空日志功能 * fix: r 变量未使用 * 增加数据库初始化数据 * fix: 移除r变量未使用
1 parent c3dceb3 commit 7a80b2b

File tree

6 files changed

+42
-3
lines changed

6 files changed

+42
-3
lines changed

controller/operation_log_controller.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@ func (m *OperationLogController) Delete(c *gin.Context) {
2424
return logic.OperationLog.Delete(c, req)
2525
})
2626
}
27+
28+
// Clean 清空记录
29+
func (m *OperationLogController) Clean(c *gin.Context) {
30+
req := new(request.OperationLogListReq)
31+
Run(c, req, func() (interface{}, interface{}) {
32+
return logic.OperationLog.Clean(c, req)
33+
})
34+
}

logic/operation_log_logic.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (l OperationLogLogic) List(c *gin.Context, req interface{}) (data interface
2121
return nil, ReqAssertErr
2222
}
2323
_ = c
24-
24+
fmt.Println(r)
2525
// 获取数据列表
2626
logs, err := isql.OperationLog.List(r)
2727
if err != nil {
@@ -72,3 +72,16 @@ func (l OperationLogLogic) Delete(c *gin.Context, req interface{}) (data interfa
7272
}
7373
return nil, nil
7474
}
75+
76+
func (l OperationLogLogic) Clean(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
77+
_, ok := req.(*request.OperationLogListReq)
78+
if !ok {
79+
return nil, ReqAssertErr
80+
}
81+
_ = c
82+
err := isql.OperationLog.Clean()
83+
if err != nil {
84+
return err, nil
85+
}
86+
return "操作日志情况完成", nil
87+
}

model/request/operation_log_req.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ type OperationLogListReq struct {
55
Username string `json:"username" form:"username"`
66
Ip string `json:"ip" form:"ip"`
77
Path string `json:"path" form:"path"`
8+
Method string `json:"method" form:"method"`
89
Status int `json:"status" form:"status"`
910
PageNum int `json:"pageNum" form:"pageNum"`
1011
PageSize int `json:"pageSize" form:"pageSize"`

public/common/init_mysql_data.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,13 @@ func InitData() {
649649
Remark: "批量删除操作日志",
650650
Creator: "系统",
651651
},
652+
{
653+
Method: "DELETE",
654+
Path: "/log/operation/clean",
655+
Category: "log",
656+
Remark: "清空操作日志",
657+
Creator: "系统",
658+
},
652659
}
653660

654661
// 5. 将角色绑定给菜单

routes/operation_log_routes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func InitOperationLogRoutes(r *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddle
1717
{
1818
operation_log.GET("/operation/list", controller.OperationLog.List)
1919
operation_log.POST("/operation/delete", controller.OperationLog.Delete)
20+
operation_log.DELETE("/operation/clean", controller.OperationLog.Clean)
2021
}
2122
return r
2223
}

service/isql/operation_log_isql.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import (
1616

1717
type OperationLogService struct{}
1818

19-
//var Logs []model.OperationLog //全局变量多个线程需要加锁,所以每个线程自己维护一个
20-
//处理OperationLogChan将日志记录到数据库
19+
// var Logs []model.OperationLog //全局变量多个线程需要加锁,所以每个线程自己维护一个
20+
// 处理OperationLogChan将日志记录到数据库
2121
func (s OperationLogService) SaveOperationLogChannel(olc <-chan *model.OperationLog) {
2222
// 只会在线程开启的时候执行一次
2323
Logs := make([]model.OperationLog, 0)
@@ -62,6 +62,10 @@ func (s OperationLogService) List(req *request.OperationLogListReq) ([]*model.Op
6262
if path != "" {
6363
db = db.Where("path LIKE ?", fmt.Sprintf("%%%s%%", path))
6464
}
65+
method := strings.TrimSpace(req.Method)
66+
if method != "" {
67+
db = db.Where("method LIKE ?", fmt.Sprintf("%%%s%%", method))
68+
}
6569
status := req.Status
6670
if status != 0 {
6771
db = db.Where("status = ?", status)
@@ -95,3 +99,8 @@ func (s OperationLogService) Exist(filter map[string]interface{}) bool {
9599
func (s OperationLogService) Delete(operationLogIds []uint) error {
96100
return common.DB.Where("id IN (?)", operationLogIds).Unscoped().Delete(&model.OperationLog{}).Error
97101
}
102+
103+
// Clean 清空日志
104+
func (s OperationLogService) Clean() error {
105+
return common.DB.Exec("TRUNCATE TABLE operation_logs").Error
106+
}

0 commit comments

Comments
 (0)