-
Notifications
You must be signed in to change notification settings - Fork 0
/
ginlogrus.go
95 lines (80 loc) · 2.25 KB
/
ginlogrus.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
package ginlogrus
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
const (
Key = "ginlogrus"
)
// New returns a gin compatable middleware using logrus to defaultValue
// skipPaths only skips the INFO loglevel
func New(logger *logrus.Logger, skipPaths ...string) gin.HandlerFunc {
var skip map[string]struct{}
if length := len(skipPaths); length > 0 {
skip = make(map[string]struct{}, length)
for _, path := range skipPaths {
skip[path] = struct{}{}
}
}
return func(c *gin.Context) {
start := time.Now()
// some evil middlewares modify this values
path := c.Request.URL.Path
logger := logger.WithFields(logrus.Fields{
"http_request_method": c.Request.Method,
"http_request_path": path,
"http_ip": c.ClientIP(),
"http_request_user-agent": c.Request.UserAgent(),
"http_request_host": c.Request.Host,
"http_request_content-length": c.Request.ContentLength,
})
SetLogger(c, logger)
c.Next()
entry := GetLogger(c)
if entry != nil {
logger = entry
}
latency := time.Now().Sub(start)
statusCode := c.Writer.Status()
entry = logger.WithFields(logrus.Fields{
"http_request_status": statusCode,
"http_request_latency": latency,
"http_request_latency_string": latency.String(),
})
if statusCode > 499 {
entry.Error(defaultValue(c.Errors.String(), statusCode))
} else if statusCode > 399 {
entry.Warn(defaultValue(c.Errors.String(), statusCode))
} else {
if _, ok := skip[path]; ok {
return
}
entry.Info(defaultValue(c.Errors.String(), statusCode))
}
}
}
// defaultValue checks if a string is empty and returns the corresponding http status text
func defaultValue(s string, code int) string {
if s == "" {
return http.StatusText(code)
}
return s
}
// GetLogger takes a gin context and returns a logrus Entry logger if it exists
// on the gin context. If it does not exist it returns nil
func GetLogger(c *gin.Context) *logrus.Entry {
logger, exists := c.Get(Key)
if !exists {
return nil
}
if l, ok := logger.(*logrus.Entry); ok {
return l
}
return nil
}
// SetLogger sets a logger on the current gin context
func SetLogger(c *gin.Context, logger *logrus.Entry) {
c.Set(Key, logger)
}