Skip to content

增加日志请求头和日志清空功能 #342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}