-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
279 lines (236 loc) · 6.89 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package logger
import (
"bytes"
"errors"
"fmt"
"github.com/ntt360/gin/core/config"
"github.com/ntt360/gin/core/gvalue"
"net"
"net/http"
"net/http/httputil"
"os"
"path"
"runtime"
"strings"
"sync"
"time"
"github.com/ntt360/gin"
"github.com/ntt360/gin/utils/uniqid"
)
// locker app global locker.
var locker sync.RWMutex
// Logger app web server log instance.
// joinLines: 是否设置为上报数据到qbus队列
func Logger(config *config.Model) gin.HandlerFunc {
conf := GinxLoggerrConfig{
Formatter: defaultLogFormatter,
SkipPaths: config.WebServerLog.SkipPaths,
}
err := initLogWriter(config)
if err != nil {
panic(err)
}
formatter := conf.Formatter
notLogged := conf.SkipPaths
var skip map[string]bool
if length := len(notLogged); length > 0 {
skip = make(map[string]bool, length)
for _, pathItem := range notLogged {
skip[pathItem] = true
}
}
return func(c *gin.Context) {
// Start timer
start := time.Now()
curPath := c.Request.URL.Path
raw := c.Request.URL.RawQuery
prefix := config.WebServerLog.TracePrefix
if len(prefix) > 0 {
prefix += "-"
}
// 自定义log TraceID, 添加时间戳方便查询
logID := c.Request.Header.Get(gvalue.HttpHeaderLogIDKey)
if logID == "" {
logID = uniqid.LogID(prefix)
c.Request.Header.Set(gvalue.HttpHeaderLogIDKey, logID)
}
// 添加响应头 Log Id
c.Header("Ginx-Id", logID)
// 程序崩溃错误收集
defer panicCatch(c, config, start, raw, curPath, formatter)
// Process request
c.Next()
// WebServerLog only when curPath is not being skipped
if _, ok := skip[curPath]; !ok {
errMsgs := c.Errors.ByType(gin.ErrorTypePrivate)
outputErrMsg := "-"
if len(errMsgs) > 0 {
errMsg, err := errMsgs.MarshalJSON()
if err != nil {
return
}
outputErrMsg = string(errMsg)
}
writeData(c, config, start, raw, curPath, formatter, outputErrMsg)
}
}
}
func getLogDir(config *config.Model) string {
logDir := strings.TrimRight(config.WebServerLog.Dir, "/")
if !path.IsAbs(logDir) {
logDir = strings.TrimRight(config.HomeDir, "/") + "/" + logDir
}
return logDir
}
func getLogBaseFileName(config *config.Model) string {
return getLogDir(config) + "/" + strings.TrimLeft(config.WebServerLog.Name, "/")
}
// 加载Log配置
func loadLogWriter(config *config.Model) error {
appLogName := getLogBaseFileName(config)
appLog, err := os.OpenFile(appLogName, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return err
}
locker.Lock()
logOut = appLog
locker.Unlock()
return nil
}
func initLogWriter(config *config.Model) error {
switch config.WebServerLog.Output {
case "file":
err := loadLogWriter(config)
if err != nil {
return err
}
case "stdout":
logOut = os.Stdout
default:
logOut = os.Stderr
}
return nil
}
func panicCatch(c *gin.Context, config *config.Model, start time.Time, raw string, path string, formatter LogFormatter) {
if err := recover(); err != nil {
var brokenPipe bool
if ne, ok := err.(*net.OpError); ok {
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
}
}
}
stack := stack(3)
httpRequest, _ := httputil.DumpRequest(c.Request, false)
headers := strings.Split(string(httpRequest), "\r\n")
for idx, header := range headers {
current := strings.Split(header, ":")
if current[0] == "Authorization" {
headers[idx] = current[0] + ": *"
}
}
panicErrString := ""
if brokenPipe {
panicErrString = fmt.Sprintf("%s\n%s", err, string(httpRequest))
} else {
panicErrString = fmt.Sprintf("panic recovered:\n%s\n%s\n%s", strings.Join(headers, "\r\n"), err, stack)
}
// 回写err, 供外层的中间件可能会使用到
_ = c.Error(errors.New(panicErrString))
// 上报数据 需要把数据变为单行数据,便于传输
if config.WebServerLog.JoinLine {
panicErrString = fmt.Sprintf("%#v", panicErrString)
}
c.AbortWithStatus(http.StatusInternalServerError)
writeData(c, config, start, raw, path, formatter, panicErrString)
}
}
func writeData(c *gin.Context, config *config.Model, start time.Time, raw string, path string, formatter LogFormatter, errMsg string) {
param := gin.LogFormatterParams{
Request: c.Request,
Keys: c.Keys,
IsTerm: config.WebServerLog.Output == "stdout" || config.WebServerLog.Output == "stderr",
}
// Stop timer
param.TimeStamp = time.Now()
param.Latency = param.TimeStamp.Sub(start)
param.ClientIP = c.ClientIP()
param.Method = c.Request.Method
param.StatusCode = c.Writer.Status()
param.ErrorMessage = errMsg
param.BodySize = c.Writer.Size()
if raw != "" {
path = path + "?" + raw
}
param.Path = path
paramWrapper := GinxLogFormatterParams{
LogFormatterParams: param,
Format: config.WebServerLog.Format,
Env: config.Env,
Idc: config.IdcName,
LocalIP: config.LocalIP,
Hostname: config.Hostname,
PrjName: config.Name,
}
_, _ = fmt.Fprint(logOut, formatter(paramWrapper))
}
// stack returns a nicely formatted stack frame, skipping skip frames.
func stack(skip int) []byte {
buf := new(bytes.Buffer) // the returned data
// As we loop, we open files and read them. These variables record the currently
// loaded file.
var lines [][]byte
var lastFile string
for i := skip; ; i++ { // Skip the expected number of frames
pc, file, line, ok := runtime.Caller(i)
if !ok {
break
}
// Print this much at least. If we can't find the source, it won't show.
_, _ = fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc)
if file != lastFile {
data, err := os.ReadFile(file)
if err != nil {
continue
}
lines = bytes.Split(data, []byte{'\n'})
lastFile = file
}
_, _ = fmt.Fprintf(buf, "\t%s: %s\n", function(pc), source(lines, line))
}
return buf.Bytes()
}
// source returns a space-trimmed slice of the n'th line.
func source(lines [][]byte, n int) []byte {
n-- // in stack trace, lines are 1-indexed but our array is 0-indexed
if n < 0 || n >= len(lines) {
return dunno
}
return bytes.TrimSpace(lines[n])
}
// function returns, if possible, the name of the function containing the PC.
func function(pc uintptr) []byte {
fn := runtime.FuncForPC(pc)
if fn == nil {
return dunno
}
name := []byte(fn.Name())
// The name includes the path name to the package, which is unnecessary
// since the file name is already included. Plus, it has center dots.
// That is, we see
// runtime/debug.*T·ptrmethod
// and want
// *T.ptrmethod
// Also the package path maybe contains dot (e.g. code.google.com/...),
// so first eliminate the path prefix
if lastSlash := bytes.LastIndex(name, slash); lastSlash >= 0 {
name = name[lastSlash+1:]
}
if period := bytes.Index(name, dot); period >= 0 {
name = name[period+1:]
}
name = bytes.Replace(name, centerDot, dot, -1)
return name
}