working commit
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package auxgin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func ResponseLogMiddleware() gin.HandlerFunc {
|
||||
return func(context *gin.Context) {
|
||||
contentType := context.GetHeader("Content-Type")
|
||||
contentType = strings.ToLower(contentType)
|
||||
|
||||
writer := &LogWriter{
|
||||
body: bytes.NewBuffer(nil),
|
||||
ResponseWriter: context.Writer,
|
||||
}
|
||||
context.Writer = writer
|
||||
|
||||
context.Next()
|
||||
|
||||
if strings.Contains(contentType, "application/json") {
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
json.Indent(buffer, writer.body.Bytes(), "", " ")
|
||||
logger := logrus.WithField("object", "responselog")
|
||||
logger.Infoln("request:\n", buffer.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user