mirror of
https://github.com/twofas/2fas-server.git
synced 2024-11-24 19:30:51 +01:00
request logger middleware: logs more information
This commit is contained in:
parent
159a281b10
commit
8024b1db77
@ -1,24 +1,39 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/twofas/2fas-server/internal/common/logging"
|
||||
"io"
|
||||
)
|
||||
|
||||
func RequestJsonLogger() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
requestLogger := logging.WithFields(logging.Fields{
|
||||
var buf bytes.Buffer
|
||||
|
||||
tee := io.TeeReader(c.Request.Body, &buf)
|
||||
body, _ := io.ReadAll(tee)
|
||||
|
||||
c.Request.Body = io.NopCloser(&buf)
|
||||
|
||||
logging.WithFields(logging.Fields{
|
||||
"client_ip": c.ClientIP(),
|
||||
"method": c.Request.Method,
|
||||
"path": c.Request.URL.Path,
|
||||
"headers": c.Request.Header,
|
||||
"body": string(body),
|
||||
"request_id": c.GetString(RequestIdKey),
|
||||
"correlation_id": c.GetString(CorrelationIdKey),
|
||||
}).Info("Request")
|
||||
|
||||
c.Next()
|
||||
|
||||
logging.WithFields(logging.Fields{
|
||||
"client_ip": c.ClientIP(),
|
||||
"method": c.Request.Method,
|
||||
"path": c.Request.URL.Path,
|
||||
"request_id": c.GetString(RequestIdKey),
|
||||
"correlation_id": c.GetString(CorrelationIdKey),
|
||||
})
|
||||
|
||||
requestLogger.Info("Request")
|
||||
|
||||
c.Next()
|
||||
|
||||
requestLogger.Info("Response")
|
||||
}).Info("Response")
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user