-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
151 lines (135 loc) · 3.74 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package logmgr
import (
"context"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"net"
"net/http"
"os"
"strings"
)
// Logger extends a logrus logger by providing path-like names.
type Logger interface {
logrus.Ext1FieldLogger
// Extend returns a Logger with an extended name path.
Extend(name string) Logger
// WithUser includes the given user id in the logger context.
WithUser(userID string) Logger
// WithGin includes the given gin request context in the logger context.
WithGin(c *gin.Context) Logger
// Recovery returns a gin handler function that will recover and log panics.
Recovery() gin.HandlerFunc
// RecoveryWith returns a gin handler function that will recover with a specific behavior
// while also logging panics.
RecoveryWith(handler gin.HandlerFunc) gin.HandlerFunc
}
type logger struct {
*logrus.Entry
level logrus.Level
manager *SentryManager
name []string
}
// NewPlainLogger creates a Logger without a SentryManager.
func NewPlainLogger(name string, level logrus.Level) Logger {
log := logrus.New()
return &logger{
Entry: log.WithField(keyLoggerName, name),
level: level,
manager: nil,
name: []string{name},
}
}
func (l *logger) Extend(name string) Logger {
log := logrus.New()
log.SetLevel(l.level)
if l.manager != nil {
log.AddHook(l.manager) // inherit manager
}
newName := append(l.name, name)
newNameStr := strings.Join(newName, ".")
return &logger{
Entry: log.WithField(keyLoggerName, newNameStr),
manager: l.manager,
name: newName,
}
}
func (l *logger) ensureContext() {
if l.Entry.Context == nil {
l.Entry.Context = context.Background()
}
}
func (l *logger) WithUser(userID string) Logger {
l.ensureContext()
l.Entry.Context = context.WithValue(l.Entry.Context, keyUserID, userID)
return &logger{
Entry: l.WithField(keyUserID, userID),
manager: l.manager,
name: l.name,
}
}
func (l *logger) WithGin(c *gin.Context) Logger {
l.ensureContext()
l.Entry.Context = context.WithValue(l.Entry.Context, keyGinContext, c)
return l
}
func (l *logger) Recovery() gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
// broken pipe handling taken from gin Recovery source code
var brokenPipe bool
if ne, ok := err.(*net.OpError); ok {
if se, ok := ne.Err.(*os.SyscallError); ok {
if strings.Contains(strings.ToLower(se.Error()), "broken pipe") || strings.Contains(strings.ToLower(se.Error()), "connection reset by peer") {
brokenPipe = true
}
}
}
if brokenPipe {
// If the connection is dead, we can't write a status to it.
_ = c.Error(err.(error))
c.Abort()
} else {
l.
WithGin(c).
WithField("panic", err).
Errorf("recovered from panic in %q", c.FullPath())
c.AbortWithStatus(http.StatusInternalServerError)
}
}
}()
c.Next()
}
}
func (l *logger) RecoveryWith(handler gin.HandlerFunc) gin.HandlerFunc {
if handler == nil {
panic("RecoveryWith handler must not be nil")
}
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
// broken pipe handling taken from gin Recovery source code
var brokenPipe bool
if ne, ok := err.(*net.OpError); ok {
if se, ok := ne.Err.(*os.SyscallError); ok {
if strings.Contains(strings.ToLower(se.Error()), "broken pipe") || strings.Contains(strings.ToLower(se.Error()), "connection reset by peer") {
brokenPipe = true
}
}
}
if brokenPipe {
// If the connection is dead, we can't write a status to it.
_ = c.Error(err.(error))
c.Abort()
} else {
l.
WithGin(c).
WithField("panic", err).
Errorf("recovered from panic in %q", c.FullPath())
handler(c)
}
}
}()
c.Next()
}
}