Skip to content

Commit

Permalink
增加日志请求头和日志清空功能 (#342)
Browse files Browse the repository at this point in the history
* feat: 增加操作日志请求头查询和清空日志功能

* fix: r 变量未使用

* 增加数据库初始化数据

* fix: 移除r变量未使用
  • Loading branch information
nangongchengfeng authored May 16, 2024
1 parent e2c2ea6 commit e825dd7
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 3 deletions.
8 changes: 8 additions & 0 deletions controller/operation_log_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ func (m *OperationLogController) Delete(c *gin.Context) {
return logic.OperationLog.Delete(c, req)
})
}

// Clean 清空记录
func (m *OperationLogController) Clean(c *gin.Context) {
req := new(request.OperationLogListReq)
Run(c, req, func() (interface{}, interface{}) {
return logic.OperationLog.Clean(c, req)
})
}
15 changes: 14 additions & 1 deletion logic/operation_log_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (l OperationLogLogic) List(c *gin.Context, req interface{}) (data interface
return nil, ReqAssertErr
}
_ = c

fmt.Println(r)
// 获取数据列表
logs, err := isql.OperationLog.List(r)
if err != nil {
Expand Down Expand Up @@ -72,3 +72,16 @@ func (l OperationLogLogic) Delete(c *gin.Context, req interface{}) (data interfa
}
return nil, nil
}

func (l OperationLogLogic) Clean(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
_, ok := req.(*request.OperationLogListReq)
if !ok {
return nil, ReqAssertErr
}
_ = c
err := isql.OperationLog.Clean()
if err != nil {
return err, nil
}
return "操作日志情况完成", nil
}
1 change: 1 addition & 0 deletions model/request/operation_log_req.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type OperationLogListReq struct {
Username string `json:"username" form:"username"`
Ip string `json:"ip" form:"ip"`
Path string `json:"path" form:"path"`
Method string `json:"method" form:"method"`
Status int `json:"status" form:"status"`
PageNum int `json:"pageNum" form:"pageNum"`
PageSize int `json:"pageSize" form:"pageSize"`
Expand Down
7 changes: 7 additions & 0 deletions public/common/init_mysql_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,13 @@ func InitData() {
Remark: "批量删除操作日志",
Creator: "系统",
},
{
Method: "DELETE",
Path: "/log/operation/clean",
Category: "log",
Remark: "清空操作日志",
Creator: "系统",
},
}

// 5. 将角色绑定给菜单
Expand Down
1 change: 1 addition & 0 deletions routes/operation_log_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func InitOperationLogRoutes(r *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddle
{
operation_log.GET("/operation/list", controller.OperationLog.List)
operation_log.POST("/operation/delete", controller.OperationLog.Delete)
operation_log.DELETE("/operation/clean", controller.OperationLog.Clean)
}
return r
}
13 changes: 11 additions & 2 deletions service/isql/operation_log_isql.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (

type OperationLogService struct{}

//var Logs []model.OperationLog //全局变量多个线程需要加锁,所以每个线程自己维护一个
//处理OperationLogChan将日志记录到数据库
// var Logs []model.OperationLog //全局变量多个线程需要加锁,所以每个线程自己维护一个
// 处理OperationLogChan将日志记录到数据库
func (s OperationLogService) SaveOperationLogChannel(olc <-chan *model.OperationLog) {
// 只会在线程开启的时候执行一次
Logs := make([]model.OperationLog, 0)
Expand Down Expand Up @@ -62,6 +62,10 @@ func (s OperationLogService) List(req *request.OperationLogListReq) ([]*model.Op
if path != "" {
db = db.Where("path LIKE ?", fmt.Sprintf("%%%s%%", path))
}
method := strings.TrimSpace(req.Method)
if method != "" {
db = db.Where("method LIKE ?", fmt.Sprintf("%%%s%%", method))
}
status := req.Status
if status != 0 {
db = db.Where("status = ?", status)
Expand Down Expand Up @@ -95,3 +99,8 @@ func (s OperationLogService) Exist(filter map[string]interface{}) bool {
func (s OperationLogService) Delete(operationLogIds []uint) error {
return common.DB.Where("id IN (?)", operationLogIds).Unscoped().Delete(&model.OperationLog{}).Error
}

// Clean 清空日志
func (s OperationLogService) Clean() error {
return common.DB.Exec("TRUNCATE TABLE operation_logs").Error
}

0 comments on commit e825dd7

Please sign in to comment.