Better logging
This commit is contained in:
parent
909dd35960
commit
35423e03d9
@ -22,7 +22,15 @@ func Logging(ctx *gin.Context) {
|
|||||||
level = sentry.LevelInfo
|
level = sentry.LevelInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
body, _ := ioutil.ReadAll(ctx.Request.Body)
|
requestBody, _ := ioutil.ReadAll(ctx.Request.Body)
|
||||||
|
|
||||||
|
var responseBody []byte
|
||||||
|
if statusCode >= 400 && statusCode <= 599 {
|
||||||
|
cw, ok := ctx.Writer.(*CustomWriter)
|
||||||
|
if ok {
|
||||||
|
responseBody = cw.Read()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
sentry.CaptureEvent(&sentry.Event{
|
sentry.CaptureEvent(&sentry.Event{
|
||||||
Extra: map[string]interface{}{
|
Extra: map[string]interface{}{
|
||||||
@ -31,7 +39,8 @@ func Logging(ctx *gin.Context) {
|
|||||||
"path": ctx.Request.URL.Path,
|
"path": ctx.Request.URL.Path,
|
||||||
"guild_id": ctx.Keys["guildid"],
|
"guild_id": ctx.Keys["guildid"],
|
||||||
"user_id": ctx.Keys["userid"],
|
"user_id": ctx.Keys["userid"],
|
||||||
"body": string(body),
|
"request_body": string(requestBody),
|
||||||
|
"response": string(responseBody),
|
||||||
},
|
},
|
||||||
Level: level,
|
Level: level,
|
||||||
Message: fmt.Sprintf("HTTP %d on %s %s", statusCode, ctx.Request.Method, ctx.FullPath()),
|
Message: fmt.Sprintf("HTTP %d on %s %s", statusCode, ctx.Request.Method, ctx.FullPath()),
|
||||||
|
31
app/http/middleware/readresponse.go
Normal file
31
app/http/middleware/readresponse.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ReadResponse(ctx *gin.Context) {
|
||||||
|
ctx.Writer = NewCustomWriter(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
type CustomWriter struct {
|
||||||
|
gin.ResponseWriter
|
||||||
|
body *bytes.Buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCustomWriter(ctx *gin.Context) *CustomWriter {
|
||||||
|
return &CustomWriter{
|
||||||
|
ResponseWriter: ctx.Writer,
|
||||||
|
body: bytes.NewBuffer([]byte{}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w CustomWriter) Write(b []byte) (int, error) {
|
||||||
|
w.body.Write(b)
|
||||||
|
return w.ResponseWriter.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *CustomWriter) Read() []byte {
|
||||||
|
return w.body.Bytes()
|
||||||
|
}
|
@ -40,7 +40,7 @@ func StartServer() {
|
|||||||
router.Use(static.Serve("/assets/", static.LocalFile("./public/static", false)))
|
router.Use(static.Serve("/assets/", static.LocalFile("./public/static", false)))
|
||||||
|
|
||||||
router.Use(gin.Recovery())
|
router.Use(gin.Recovery())
|
||||||
router.Use(middleware.MultiReadBody)
|
router.Use(middleware.MultiReadBody, middleware.ReadResponse)
|
||||||
router.Use(middleware.Logging)
|
router.Use(middleware.Logging)
|
||||||
router.Use(sentrygin.New(sentrygin.Options{})) // Defaults are ok
|
router.Use(sentrygin.New(sentrygin.Options{})) // Defaults are ok
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user