数据结构优化
This commit is contained in:
@@ -49,7 +49,25 @@ func OperationLogMiddleware() gin.HandlerFunc {
|
||||
|
||||
// 获取IP归属地
|
||||
ip := c.ClientIP()
|
||||
region := utils.GetRegion(ip)
|
||||
var region string
|
||||
// 使用 recover 保护 GetRegion 调用
|
||||
func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Printf("Panic in GetRegion for IP %s (operation log): %v", ip, r)
|
||||
region = "Unknown"
|
||||
}
|
||||
}()
|
||||
region = utils.GetRegion(ip)
|
||||
}()
|
||||
|
||||
// 确保 region 不为空,如果为空则设置为 "Unknown"
|
||||
if region == "" {
|
||||
region = "Unknown"
|
||||
}
|
||||
|
||||
// 记录调试信息
|
||||
log.Printf("Creating operation log: UserID=%d, IP=%s, Region=%s, Path=%s", userID.(uint), ip, region, c.Request.URL.Path)
|
||||
|
||||
// 构建操作日志
|
||||
operationLog := &models.OperationLog{
|
||||
@@ -66,8 +84,16 @@ func OperationLogMiddleware() gin.HandlerFunc {
|
||||
|
||||
// 异步记录日志,避免影响响应
|
||||
go func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Printf("Panic in CreateOperationLog goroutine: %v", r)
|
||||
}
|
||||
}()
|
||||
|
||||
if err := repositories.CreateOperationLog(operationLog); err != nil {
|
||||
log.Printf("Error creating operation log: %v", err)
|
||||
log.Printf("Error creating operation log for UserID=%d, IP=%s, Region=%s: %v", userID.(uint), ip, region, err)
|
||||
} else {
|
||||
log.Printf("Successfully created operation log: UserID=%d, IP=%s, Region=%s", userID.(uint), ip, region)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user