-
Notifications
You must be signed in to change notification settings - Fork 14
/
ginlogger.go
31 lines (28 loc) · 892 Bytes
/
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
package main
import (
"context"
"fmt"
"github.com/axiaoxin-com/logging"
"github.com/gin-gonic/gin"
)
func main() {
gin.SetMode(gin.ReleaseMode)
app := gin.New()
// you can custom the config or use logging.GinLogger() by default config
conf := logging.GinLoggerConfig{
Formatter: func(c context.Context, m logging.GinLogDetails) string {
return fmt.Sprintf("%s use %s request %s at %v, handler %s use %f seconds to respond it with %d",
m.ClientIP, m.Method, m.RequestURI, m.ReqTime, m.HandlerName, m.Latency, m.StatusCode)
},
SkipPaths: []string{},
EnableDetails: false,
TraceIDFunc: func(context.Context) string { return "fake-uuid" },
}
app.Use(logging.GinLoggerWithConfig(conf))
app.POST("/ping", func(c *gin.Context) {
// panic("xx")
// time.Sleep(300 * time.Millisecond)
c.JSON(200, string(logging.GetGinRequestBody(c)))
})
app.Run(":8888")
}