mirror of
https://github.com/lzh-1625/go_process_manager.git
synced 2025-10-05 07:56:50 +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...)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -9,7 +9,6 @@ import (
|
|||||||
"github.com/lzh-1625/go_process_manager/internal/app/repository/query"
|
"github.com/lzh-1625/go_process_manager/internal/app/repository/query"
|
||||||
|
|
||||||
"github.com/glebarez/sqlite"
|
"github.com/glebarez/sqlite"
|
||||||
"gorm.io/gen"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"gorm.io/gorm/logger"
|
"gorm.io/gorm/logger"
|
||||||
)
|
)
|
||||||
@@ -42,15 +41,15 @@ func InitDb() {
|
|||||||
}
|
}
|
||||||
sqlDB.SetConnMaxLifetime(time.Hour)
|
sqlDB.SetConnMaxLifetime(time.Hour)
|
||||||
db = gdb.Session(&defaultConfig)
|
db = gdb.Session(&defaultConfig)
|
||||||
// db = gdb.Session(&defaultConfig).Debug()
|
// db = db.Debug()
|
||||||
db.AutoMigrate(&model.Process{}, &model.User{}, &model.Permission{}, &model.Push{}, &model.Config{}, &model.ProcessLog{}, &model.Task{}, &model.WsShare{})
|
db.AutoMigrate(&model.Process{}, &model.User{}, &model.Permission{}, &model.Push{}, &model.Config{}, &model.ProcessLog{}, &model.Task{}, &model.WsShare{})
|
||||||
|
|
||||||
g := gen.NewGenerator(gen.Config{
|
// g := gen.NewGenerator(gen.Config{
|
||||||
OutPath: "internal/app/repository/query",
|
// OutPath: "internal/app/repository/query",
|
||||||
Mode: gen.WithoutContext | gen.WithDefaultQuery | gen.WithQueryInterface, // generate mode
|
// Mode: gen.WithoutContext | gen.WithDefaultQuery | gen.WithQueryInterface, // generate mode
|
||||||
})
|
// })
|
||||||
g.UseDB(db)
|
// g.UseDB(db)
|
||||||
g.ApplyBasic(&model.Process{}, &model.User{}, &model.Permission{}, &model.Push{}, &model.Config{}, &model.ProcessLog{}, &model.Task{}, &model.WsShare{})
|
// g.ApplyBasic(&model.Process{}, &model.User{}, &model.Permission{}, &model.Push{}, &model.Config{}, &model.ProcessLog{}, &model.Task{}, &model.WsShare{})
|
||||||
g.Execute()
|
// g.Execute()
|
||||||
query.SetDefault(db)
|
query.SetDefault(db)
|
||||||
}
|
}
|
||||||
|
@@ -65,7 +65,7 @@ func (p *permissionRepository) GetPermission(user string, pid int) (result model
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *permissionRepository) GetProcessNameByPermission(user string, op constants.OprPermission) (result []string) {
|
func (p *permissionRepository) GetProcessNameByPermission(user string, op constants.OprPermission) (result []string) {
|
||||||
tx := query.Permission.Select(query.Process.Name).RightJoin(query.Permission, query.Process.Uuid.EqCol(query.Permission.Pid)).Where(query.Permission.Account.Eq(user)).Where(query.Permission.Owned.Is(true))
|
tx := query.Permission.Select(query.Process.Name).RightJoin(query.Process, query.Process.Uuid.EqCol(query.Permission.Pid)).Where(query.Permission.Account.Eq(user)).Where(query.Permission.Owned.Is(true))
|
||||||
switch op {
|
switch op {
|
||||||
case constants.OPERATION_LOG:
|
case constants.OPERATION_LOG:
|
||||||
tx = tx.Where(query.Permission.Log.Is(true))
|
tx = tx.Where(query.Permission.Log.Is(true))
|
||||||
|
@@ -20,7 +20,7 @@ func Route() {
|
|||||||
r := gin.New()
|
r := gin.New()
|
||||||
r.Use(gin.Recovery())
|
r.Use(gin.Recovery())
|
||||||
if !config.CF.Tui {
|
if !config.CF.Tui {
|
||||||
r.Use(gin.Logger())
|
r.Use(middle.Logger())
|
||||||
}
|
}
|
||||||
routePathInit(r)
|
routePathInit(r)
|
||||||
staticInit(r)
|
staticInit(r)
|
||||||
|
Reference in New Issue
Block a user