/
jsonlogger.go
54 lines (49 loc) · 1.24 KB
/
jsonlogger.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
package middleware
import (
"time"
"github.com/bytedance/sonic"
"github.com/gin-gonic/gin"
"github.com/lyhgo/gin/tool/logging"
)
type RequestLog struct {
Level string
ClientIP string
TS string
Method string
Path string
Proto string
StatusCode int
Latency float64
UserAgent string
ErrorMessage string
}
func JsonLogger() gin.HandlerFunc {
return jsonLoggerHandler()
}
// jsonLoggerHandler returns a middleware to record json format request log.
func jsonLoggerHandler() gin.HandlerFunc {
logger := logging.GetLogger()
return gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
r := RequestLog{
Level: "info",
ClientIP: param.ClientIP,
TS: param.TimeStamp.Format(time.RFC1123),
Method: param.Method,
Path: param.Path,
Proto: param.Request.Proto,
StatusCode: param.StatusCode,
Latency: param.Latency.Seconds(),
UserAgent: param.Request.UserAgent(),
ErrorMessage: param.ErrorMessage,
}
if r.ErrorMessage != "" {
r.Level = "error"
}
logBytes, err := sonic.Marshal(&r)
if err != nil {
logger.Error(err.Error())
}
logBytes = append(logBytes, '\n')
return string(logBytes)
})
}