2023-11-22 22:21:11 +03:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2023-11-22 23:31:35 +03:00
|
|
|
"github.com/Neur0toxine/sshpoke/internal/api/rest/middleware/log"
|
2023-11-22 22:21:11 +03:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
func SetupLogger(zapLog *zap.SugaredLogger) gin.HandlerFunc {
|
|
|
|
gin.DefaultWriter = log.AsWriter(zapLog, zap.DebugLevel)
|
|
|
|
gin.DefaultErrorWriter = log.AsWriter(zapLog, zap.ErrorLevel)
|
|
|
|
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
start := time.Now()
|
|
|
|
c.Next()
|
|
|
|
now := time.Now()
|
|
|
|
zapLog.Debugw(fmt.Sprintf("%d %s | %s %s",
|
|
|
|
c.Writer.Status(), http.StatusText(c.Writer.Status()), c.Request.Method, c.Request.URL.String()),
|
|
|
|
"ts", now,
|
|
|
|
"latency", now.Sub(start),
|
|
|
|
"clientIp", c.ClientIP(),
|
|
|
|
"method", c.Request.Method,
|
|
|
|
"status", c.Writer.Status(),
|
|
|
|
"error", c.Errors.ByType(gin.ErrorTypePrivate).String(),
|
|
|
|
"bodyLength", c.Writer.Size())
|
|
|
|
}
|
|
|
|
}
|