mirror of
https://github.com/lzh-1625/go_process_manager.git
synced 2025-10-07 17:01:05 +08:00
add gin logger
This commit is contained in:
33
internal/app/middle/logger.go
Normal file
33
internal/app/middle/logger.go
Normal file
@@ -0,0 +1,33 @@
|
||||
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, "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...)
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user