-
Notifications
You must be signed in to change notification settings - Fork 0
/
ginlogger.go
50 lines (47 loc) · 1.73 KB
/
ginlogger.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
package main
import (
"fmt"
"time"
"github.com/feymanlee/logit"
"github.com/gin-gonic/gin"
)
func main() {
gin.SetMode(gin.ReleaseMode)
app := gin.New()
// you can custom the config or use logit.GinLogger() by default config
conf := logit.GinLoggerConfig{
Name: "access",
Formatter: func(c *gin.Context, ext logit.GinLogExtends) string {
return fmt.Sprintf("%s use %s request %s at %v, handler %s use %f seconds to respond it with %d",
c.ClientIP(),
c.Request.Method,
c.Request.Host,
c.Request.RequestURI,
ext.HandleName,
ext.Latency,
c.Writer.Status())
},
SkipPaths: []string{"/user/list"},
EnableDetails: false,
TraceIDFunc: func(c *gin.Context) string { return "my-trace-id" },
SkipPathRegexps: []string{"/user/.*?"},
EnableContextKeys: false, // 记录 context 里面的 key
EnableRequestHeader: false, // 记录 header
EnableRequestForm: false, // 记录 request form
EnableRequestBody: false, // 记录 request body
EnableResponseBody: false, // 记录 response body
SlowThreshold: time.Second, // 慢查询阈值,超时这个时间会答应 Warn 日志
OutputPaths: []string{"stdout", "lumberjack:"},
InitialFields: map[string]interface{}{"key1": "value1"}, // 一些初始化的打印字段
DisableCaller: false, // 禁用 caller 打印
DisableStacktrace: false, // 禁用 Stacktrace
EncoderConfig: nil,
}
app.Use(logit.NewGinLogger(conf))
app.POST("/ping", func(c *gin.Context) {
// panic("xx")
// time.Sleep(300 * time.Millisecond)
c.JSON(200, string(logit.GetGinRequestBody(c)))
})
app.Run(":8888")
}