44 lines
883 B
Go
44 lines
883 B
Go
package middlewares
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"enaklo-pos-be/internal/common/request"
|
|
)
|
|
|
|
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
|
|
|
|
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",
|
|
param.TimeStamp.Format(time.RFC1123),
|
|
parsedReqInfo.TraceId,
|
|
parsedReqInfo.UserId,
|
|
param.Method,
|
|
param.Path,
|
|
param.StatusCode,
|
|
param.Latency,
|
|
)
|
|
})
|
|
}
|