-
Notifications
You must be signed in to change notification settings - Fork 1
/
gin.go
96 lines (92 loc) · 2.24 KB
/
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 adapter
import (
"errors"
"fmt"
"github.com/TwiN/go-color"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"net"
"net/http/httputil"
"os"
"runtime/debug"
"strings"
"time"
)
func GinLogger() gin.HandlerFunc {
renderStatus := func(status int) string {
s := fmt.Sprintf(" %d ", status)
switch {
case status == 200:
return color.InWhiteOverCyan(s)
default:
return color.InWhiteOverYellow(s)
}
}
return func(c *gin.Context) {
start := time.Now()
path := c.Request.URL.Path
query := c.Request.URL.RawQuery
c.Next()
duration := time.Since(start)
if skip, exists := c.Get("skip_logging"); exists && skip.(bool) {
c.Next()
return
}
zap.L().Info(fmt.Sprintf(
"[%s] %s %s",
color.InCyan("GIN"),
renderStatus(c.Writer.Status()),
color.InBold(path)),
zap.Int("status", c.Writer.Status()),
zap.String("method", c.Request.Method),
zap.String("path", path),
zap.String("query", query),
zap.String("ip", c.ClientIP()),
zap.String("user-agent", c.Request.UserAgent()),
zap.String("errors", c.Errors.ByType(gin.ErrorTypePrivate).String()),
zap.Duration("duration", duration),
)
}
}
func GinRecovery(stack bool) gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
var brokenPipe bool
var ne *net.OpError
if errors.As(err.(error), &ne) {
var se *os.SyscallError
if errors.As(ne.Err, &se) {
if strings.Contains(strings.ToLower(se.Error()), "broken pipe") || strings.Contains(strings.ToLower(se.Error()), "connection reset by peer") {
brokenPipe = true
}
}
}
httpRequest, _ := httputil.DumpRequest(c.Request, false)
if brokenPipe {
zap.L().Error(c.Request.URL.Path,
zap.Any("error", err),
zap.String("request", string(httpRequest)),
)
_ = c.Error(err.(error))
c.Abort()
return
}
if stack {
zap.L().Error("Recovery from panic.",
zap.Any("error", err),
zap.String("request", string(httpRequest)),
zap.String("stack", string(debug.Stack())),
)
} else {
zap.L().Error("Recovery from panic.",
zap.Any("error", err),
zap.String("request", string(httpRequest)),
)
}
c.Abort()
}
}()
c.Next()
}
}