hotgo/server/internal/logic/hook/access_log.go

40 lines
945 B
Go
Raw Normal View History

2022-11-24 23:37:34 +08:00
// Package hook
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2022 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
//
package hook
import (
2023-02-09 14:35:35 +08:00
"context"
2022-11-24 23:37:34 +08:00
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gtime"
"hotgo/internal/library/contexts"
"hotgo/internal/service"
2023-02-09 14:35:35 +08:00
"hotgo/utility/simple"
2022-11-24 23:37:34 +08:00
)
2023-02-08 20:29:34 +08:00
// AccessLog 访问日志
func (s *sHook) AccessLog(r *ghttp.Request) {
2022-11-24 23:37:34 +08:00
var (
ctx = r.Context()
)
// 没有上下文的请求不记录doc、favicon.ico等非功能类业务
modelCtx := contexts.Get(ctx)
if modelCtx == nil {
return
}
// 计算运行耗时
contexts.SetTakeUpTime(ctx, gtime.TimestampMilli()-r.EnterTime)
2023-02-09 14:35:35 +08:00
simple.SafeGo(ctx, func(ctx context.Context) {
2022-11-24 23:37:34 +08:00
if err := service.SysLog().AutoLog(ctx); err != nil {
2023-02-09 14:35:35 +08:00
g.Log().Warningf(ctx, "hook AccessLog err:%+v", err)
2022-11-24 23:37:34 +08:00
}
2023-02-09 14:35:35 +08:00
})
2022-11-24 23:37:34 +08:00
}