169 lines
4.4 KiB
Go
169 lines
4.4 KiB
Go
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
|
|
"easygoadmin/internal/consts"
|
|
"easygoadmin/internal/router/meta"
|
|
"easygoadmin/utility/response"
|
|
"easygoadmin/utility/ua"
|
|
)
|
|
|
|
// ============================== 操作日志中间件 ==============================
|
|
//
|
|
// 请求结束后根据路由元数据(gmeta log/logType 标签)组装记录,
|
|
// 经有界通道异步批量落库,不阻塞业务请求。
|
|
//
|
|
// 开关:operationLog.enable 为总开关;operationLog.logQuery 控制查询类是否记录。
|
|
|
|
const (
|
|
logChanSize = 2048 // 日志通道容量
|
|
logBatchSize = 100 // 单批落库条数
|
|
logFlushEvery = time.Second // 定时刷新间隔
|
|
logMaxLen = 8000 // param/result 截断长度
|
|
)
|
|
|
|
var (
|
|
logOnce sync.Once // 保证日志工作协程仅启动一次
|
|
logChan = make(chan *OperationLogEntry, logChanSize) // 日志缓冲通道
|
|
logDrop int64 // 累计丢弃条数
|
|
logMu sync.Mutex // 丢弃计数互斥锁
|
|
)
|
|
|
|
// OperationLog 操作日志采集
|
|
//
|
|
// 依据路由元数据判断是否记录,组装日志记录后投入异步通道
|
|
func OperationLog(r *ghttp.Request) {
|
|
if !cfgBool(r.Context(), "operationLog.enable", true) {
|
|
r.Middleware.Next()
|
|
return
|
|
}
|
|
start := time.Now()
|
|
r.Middleware.Next()
|
|
|
|
rm, ok := r.Context().Value(meta.CtxKey).(meta.RouteMeta)
|
|
if !ok || rm.Log == "" {
|
|
return
|
|
}
|
|
logType := consts.LogTypeMap[rm.LogType]
|
|
if logType == consts.LogTypeQuery && !cfgBool(r.Context(), "operationLog.logQuery", true) {
|
|
return
|
|
}
|
|
|
|
code, _ := response.GetCode(r)
|
|
entry := &OperationLogEntry{
|
|
Title: rm.Log,
|
|
Type: logType,
|
|
Source: 0,
|
|
Method: rm.Method + " " + rm.Path,
|
|
RequestMethod: r.Method,
|
|
URL: r.URL.String(),
|
|
Param: trunc(collectParam(r), logMaxLen),
|
|
Result: trunc(collectResult(r), logMaxLen),
|
|
IP: realClientIP(r),
|
|
Location: requestLocation(r.Context(), realClientIP(r)),
|
|
OS: ua.ParseOS(r.Header.Get("User-Agent")),
|
|
Browser: ua.ParseBrowser(r.Header.Get("User-Agent")),
|
|
ConsumeTime: time.Since(start).Milliseconds(),
|
|
Status: 0,
|
|
Error: "",
|
|
CreateUser: consts.GetUsername(r.Context()),
|
|
}
|
|
if code != 0 {
|
|
entry.Status = 1
|
|
if body, ok := response.GetBody(r); ok && body.Msg != "" {
|
|
entry.Error = trunc(body.Msg, 500)
|
|
}
|
|
}
|
|
|
|
logOnce.Do(startLogWorker)
|
|
select {
|
|
case logChan <- entry:
|
|
default:
|
|
logMu.Lock()
|
|
logDrop++
|
|
n := logDrop
|
|
logMu.Unlock()
|
|
if n%100 == 1 {
|
|
g.Log().Warningf(r.Context(), "操作日志通道已满,累计丢弃 %d 条", n)
|
|
}
|
|
}
|
|
}
|
|
|
|
// collectParam 组装脱敏后的请求参数
|
|
//
|
|
// GET 与 DELETE 请求取 query 参数,写操作优先取请求体
|
|
func collectParam(r *ghttp.Request) string {
|
|
if r.Method == "GET" || r.Method == "DELETE" {
|
|
m := r.GetMap()
|
|
if len(m) == 0 {
|
|
return ""
|
|
}
|
|
return trunc(gjsonEncode(m), logMaxLen)
|
|
}
|
|
body := r.GetBodyString()
|
|
if body != "" {
|
|
return maskSecret(body)
|
|
}
|
|
return trunc(gjsonEncode(r.GetMap()), logMaxLen)
|
|
}
|
|
|
|
// collectResult 组装响应信封内容
|
|
func collectResult(r *ghttp.Request) string {
|
|
body, ok := response.GetBody(r)
|
|
if !ok {
|
|
return ""
|
|
}
|
|
return trunc(gjsonEncode(body), logMaxLen)
|
|
}
|
|
|
|
// maskSecret 脱敏敏感字段
|
|
//
|
|
// 覆盖密码、盐与令牌等字段
|
|
func maskSecret(s string) string {
|
|
for _, k := range []string{"password", "Password", "oldPassword", "newPassword", "salt", "access_token", "refresh_token"} {
|
|
s = maskJSONField(s, k)
|
|
}
|
|
return s
|
|
}
|
|
|
|
// startLogWorker 启动异步落库工作协程
|
|
//
|
|
// 按批量大小或定时触发刷新,将缓冲日志写入 LogSink
|
|
func startLogWorker() {
|
|
go func() {
|
|
batch := make([]*OperationLogEntry, 0, logBatchSize)
|
|
flush := func() {
|
|
if len(batch) == 0 || LogSink == nil {
|
|
batch = batch[:0]
|
|
return
|
|
}
|
|
ctx := context.Background()
|
|
for _, e := range batch {
|
|
if err := LogSink(ctx, e); err != nil {
|
|
g.Log().Errorf(ctx, "操作日志落库失败: %v", err)
|
|
}
|
|
}
|
|
batch = batch[:0]
|
|
}
|
|
ticker := time.NewTicker(logFlushEvery)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case e := <-logChan:
|
|
batch = append(batch, e)
|
|
if len(batch) >= logBatchSize {
|
|
flush()
|
|
}
|
|
case <-ticker.C:
|
|
flush()
|
|
}
|
|
}
|
|
}()
|
|
}
|