Files
go_process_manager/internal/app/middle/logger.go
akrike 52616e29d9 1
2025-06-21 16:02:35 +08:00

35 lines
808 B
Go

package middle
import (
"fmt"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/lzh-1625/go_process_manager/internal/app/constants"
"github.com/lzh-1625/go_process_manager/log"
)
func Logger() gin.HandlerFunc {
return func(ctx *gin.Context) {
start := time.Now()
path := ctx.Request.URL.Path
if !strings.HasPrefix(path, "/api") {
return
}
// Process request
ctx.Next()
logKv := []any{}
logKv = append(logKv, "Method", ctx.Request.Method)
logKv = append(logKv, "Status", ctx.Writer.Status())
logKv = append(logKv, "Path", path)
logKv = append(logKv, "耗时", fmt.Sprintf("%dms", time.Now().UnixMilli()-start.UnixMilli()))
if user, ok := ctx.Get(constants.CTXFLG_USER_NAME); ok {
logKv = append(logKv, "user", user)
}
log.Logger.Infow("GIN", logKv...)
}
}