-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
60 lines (50 loc) · 991 Bytes
/
log.go
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
57
58
59
60
package main
import (
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"os"
"time"
)
var logger *log.Entry
func initLog() {
l := log.New()
if Config.Debug {
l.SetLevel(log.DebugLevel)
} else {
l.SetLevel(log.InfoLevel)
}
// 输出格式是普通文字格式
l.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
l.SetOutput(os.Stdout)
logger = log.NewEntry(l)
}
func getGinLogFun() func(c *gin.Context) {
return func(c *gin.Context) {
// 开始时间
startTime := time.Now()
// 处理请求
c.Next()
// 结束时间
endTime := time.Now()
// 执行时间
latencyTime := endTime.Sub(startTime)
// 请求方式
reqMethod := c.Request.Method
// 请求路由
reqUri := c.Request.RequestURI
// 状态码
statusCode := c.Writer.Status()
// 请求IP
clientIP := c.ClientIP()
// 日志格式
logger.Debugf("| %3d | %13v | %15s | %s | %s |",
statusCode,
latencyTime,
clientIP,
reqMethod,
reqUri,
)
}
}