diff --git a/app/http/middleware/logging.go b/app/http/middleware/logging.go index be437ff..d46b8b3 100644 --- a/app/http/middleware/logging.go +++ b/app/http/middleware/logging.go @@ -22,7 +22,15 @@ func Logging(ctx *gin.Context) { 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{ Extra: map[string]interface{}{ @@ -31,7 +39,8 @@ func Logging(ctx *gin.Context) { "path": ctx.Request.URL.Path, "guild_id": ctx.Keys["guildid"], "user_id": ctx.Keys["userid"], - "body": string(body), + "request_body": string(requestBody), + "response": string(responseBody), }, Level: level, Message: fmt.Sprintf("HTTP %d on %s %s", statusCode, ctx.Request.Method, ctx.FullPath()), diff --git a/app/http/middleware/readresponse.go b/app/http/middleware/readresponse.go new file mode 100644 index 0000000..42a67e8 --- /dev/null +++ b/app/http/middleware/readresponse.go @@ -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() +} diff --git a/app/http/server.go b/app/http/server.go index 98f193b..c3b7b6c 100644 --- a/app/http/server.go +++ b/app/http/server.go @@ -40,7 +40,7 @@ func StartServer() { router.Use(static.Serve("/assets/", static.LocalFile("./public/static", false))) router.Use(gin.Recovery()) - router.Use(middleware.MultiReadBody) + router.Use(middleware.MultiReadBody, middleware.ReadResponse) router.Use(middleware.Logging) router.Use(sentrygin.New(sentrygin.Options{})) // Defaults are ok