working commit
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package auxgin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func RequestLogMiddleware() gin.HandlerFunc {
|
||||
return func(context *gin.Context) {
|
||||
|
||||
contentType := context.GetHeader("Content-Type")
|
||||
contentType = strings.ToLower(contentType)
|
||||
|
||||
var requestBody []byte
|
||||
if context.Request.Body != nil {
|
||||
requestBody, _ = ioutil.ReadAll(context.Request.Body)
|
||||
}
|
||||
|
||||
if strings.Contains(contentType, "application/json") && context.Request.Method == "POST" {
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
json.Indent(buffer, requestBody, "", " ")
|
||||
logger := logrus.WithField("object", "requestlog")
|
||||
logger.Infoln("request:\n", buffer.String())
|
||||
}
|
||||
|
||||
context.Request.Body = ioutil.NopCloser(bytes.NewReader(requestBody))
|
||||
context.Next()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user