/
logger.go
88 lines (75 loc) · 2.62 KB
/
logger.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
package middleware
import (
"fmt"
"mime"
"net/http"
"time"
"github.com/LyricTian/gin-admin/v10/pkg/logging"
"github.com/LyricTian/gin-admin/v10/pkg/util"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
type LoggerConfig struct {
AllowedPathPrefixes []string
SkippedPathPrefixes []string
MaxOutputRequestBodyLen int
MaxOutputResponseBodyLen int
}
var DefaultLoggerConfig = LoggerConfig{
MaxOutputRequestBodyLen: 1024 * 1024,
MaxOutputResponseBodyLen: 1024 * 1024,
}
// Record detailed request logs for quick troubleshooting.
func Logger() gin.HandlerFunc {
return LoggerWithConfig(DefaultLoggerConfig)
}
func LoggerWithConfig(config LoggerConfig) gin.HandlerFunc {
return func(c *gin.Context) {
if !AllowedPathPrefixes(c, config.AllowedPathPrefixes...) ||
SkippedPathPrefixes(c, config.SkippedPathPrefixes...) {
c.Next()
return
}
start := time.Now()
contentType := c.Request.Header.Get("Content-Type")
fields := []zap.Field{
zap.String("client_ip", c.ClientIP()),
zap.String("method", c.Request.Method),
zap.String("path", c.Request.URL.Path),
zap.String("user_agent", c.Request.UserAgent()),
zap.String("referer", c.Request.Referer()),
zap.String("uri", c.Request.RequestURI),
zap.String("host", c.Request.Host),
zap.String("remote_addr", c.Request.RemoteAddr),
zap.String("proto", c.Request.Proto),
zap.Int64("content_length", c.Request.ContentLength),
zap.String("content_type", contentType),
zap.String("pragma", c.Request.Header.Get("Pragma")),
}
c.Next()
if c.Request.Method == http.MethodPost || c.Request.Method == http.MethodPut {
mediaType, _, _ := mime.ParseMediaType(contentType)
if mediaType == "application/json" {
if v, ok := c.Get(util.ReqBodyKey); ok {
if b, ok := v.([]byte); ok && len(b) <= config.MaxOutputRequestBodyLen {
fields = append(fields, zap.String("body", string(b)))
}
}
}
}
cost := time.Since(start).Nanoseconds() / 1e6
fields = append(fields, zap.Int64("cost", cost))
fields = append(fields, zap.Int("status", c.Writer.Status()))
fields = append(fields, zap.String("res_time", time.Now().Format("2006-01-02 15:04:05.999")))
fields = append(fields, zap.Int("res_size", c.Writer.Size()))
if v, ok := c.Get(util.ResBodyKey); ok {
if b, ok := v.([]byte); ok && len(b) <= config.MaxOutputResponseBodyLen {
fields = append(fields, zap.String("res_body", string(b)))
}
}
ctx := c.Request.Context()
ctx = logging.NewTag(ctx, logging.TagKeyRequest)
logging.Context(ctx).Info(fmt.Sprintf("[HTTP] %s-%s-%d (%dms)",
c.Request.URL.Path, c.Request.Method, c.Writer.Status(), cost), fields...)
}
}