-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgollog_gin.go
96 lines (80 loc) · 2 KB
/
gollog_gin.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package gollog
/*
Helper GinLog to show request details
*/
import (
"fmt"
"log"
"math"
"time"
"github.com/gin-gonic/gin"
jsoniter "github.com/json-iterator/go"
)
type ginLog struct {
LogLevel string
HttpStatusCode int `json:"status,omitempty"`
HttpMethod string `json:",omitempty"`
HttpPath string `json:"path,omitempty"`
AfterMicroseconds int `json:"usec,omitempty"`
DataLength int `json:"len,omitempty"`
ClientIp string `json:"src,omitempty"`
UserAgent string `json:"user-agent,omitempty"`
HttpReferer string `json:"referer,omitempty"`
RequestedAt string `json:"at,omitempty"`
}
var (
json = jsoniter.ConfigCompatibleWithStandardLibrary
GinTimeFormat = time.RFC3339
GinEnableUTC = false
)
func ginLogger(ctx *gin.Context) {
startCtx := time.Now()
path := ctx.Request.URL.Path
ctx.Next()
stopCtx := time.Since(startCtx)
msUsed := int(math.Ceil(float64(stopCtx.Nanoseconds()) / 1000.0))
statusCode := ctx.Writer.Status()
clientIP := ctx.ClientIP()
clientUserAgent := ctx.Request.UserAgent()
referer := ctx.Request.Referer()
dataLength := ctx.Writer.Size()
httpMethod := ctx.Request.Method
if dataLength < 0 {
dataLength = 0
}
if GinEnableUTC {
startCtx = startCtx.UTC()
}
startTime := startCtx.Format(GinTimeFormat)
entry := ginLog{
HttpStatusCode: statusCode,
HttpMethod: httpMethod,
HttpPath: path,
AfterMicroseconds: msUsed,
DataLength: dataLength,
ClientIp: clientIP,
UserAgent: clientUserAgent,
HttpReferer: referer,
RequestedAt: startTime,
}
if len(ctx.Errors) > 0 {
entry.LogLevel = "PANIC"
} else {
if statusCode > 499 {
entry.LogLevel = "ERROR"
} else if statusCode > 399 {
entry.LogLevel = "WARN"
} else {
entry.LogLevel = "INFO"
}
}
logBytes, err := json.Marshal(&entry)
if err != nil {
log.Println(err)
return
}
fmt.Println(string(logBytes))
}
func SetGinLog(router *gin.Engine) {
router.Use(ginLogger, gin.Recovery())
}