1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package logs
import (
"fmt"
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/glog"
)
type RequestLogReq struct {
Path string //存储路径
RequestURI string //请求URI
RequestID string //请求RequestID
Method string //请求方法
Params string //请求参数
Response string //响应参数
Err error //错误信息
ServerName string //请求服务名称
ResponseTime int64 //响应时间 毫秒级
}
//统一请求日志 20211208 gk
func RequestLog(req RequestLogReq){
Info(req.Path, "请求ID:【%v】 服务名称: 【%v】 请求路径:【%v】 请求方法: 【%v】 请求参数: 【%v】 响应参数: 【%v】 响应时间:【%v ms】error:【%v】",
req.RequestID, req.ServerName, req.RequestURI, req.Method, req.Params,req.Response, req.ResponseTime, nil)
if req.Err != nil {
Error("%+v", gerror.Wrap(req.Err, req.RequestID))
}
}
//记录info日志 20211208 gk
func Info(path string,format string, v ...interface{}) {
newPath := g.Log().GetPath() + "/" + path
glog.Path(newPath).File( "info-{Ymd}.log").Println(fmt.Sprintf(format, v...))
}
//记录error日志 20211208 gk
func Error(format string, v ...interface{}) {
newPath := g.Log().GetPath() + "/error/"
glog.Path(newPath).File("{Ymd}.log").Println(fmt.Sprintf(format, v...))
}
//检查错误 20211208 gk
func CheckErr(err error, extra string) bool {
if err != nil {
Error("%+v",gerror.Wrap(err, extra))
return true
}
return false
}