This commit is contained in:
aditya.siregar
2024-08-10 23:52:09 +07:00
parent 2fd676ef3e
commit 414c980c13
12 changed files with 68 additions and 28 deletions
+17 -5
View File
@@ -9,16 +9,28 @@ import (
"furtuna-be/internal/common/request"
)
func Logger() gin.HandlerFunc {
type ConfigLogger interface {
IsLoggerEnabled() bool
}
func Logger(c ConfigLogger) gin.HandlerFunc {
if !c.IsLoggerEnabled() {
return func(ctx *gin.Context) {
ctx.Next()
}
}
return gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
var parsedReqInfo request.RequestInfo
reqInfo, exists := param.Keys[request.ReqInfoKey]
if exists {
parsedReqInfo = reqInfo.(request.RequestInfo)
if reqInfo, exists := param.Keys[request.ReqInfoKey]; exists {
if castedReqInfo, ok := reqInfo.(request.RequestInfo); ok {
parsedReqInfo = castedReqInfo
}
}
return fmt.Sprintf("%s - [HTTP] TraceId: %s; UserId: %d; Method: %s; Path: %s; Status: %d, Latency: %s;\n\n",
return fmt.Sprintf(
"%s - [HTTP] TraceId: %s; UserId: %d; Method: %s; Path: %s; Status: %d; Latency: %s;\n\n",
param.TimeStamp.Format(time.RFC1123),
parsedReqInfo.TraceId,
parsedReqInfo.UserId,
+7 -1
View File
@@ -16,7 +16,13 @@ import (
"furtuna-be/internal/common/logger"
)
func RequestMiddleware() (handler gin.HandlerFunc) {
func RequestMiddleware(c ConfigLogger) (handler gin.HandlerFunc) {
if !c.IsLoggerEnabled() {
return func(ctx *gin.Context) {
ctx.Next()
}
}
return func(ctx *gin.Context) {
start := time.Now()
body, _ := readRequestBody(ctx.Request)