sshpoke/internal/api/web/middleware/log.go

32 lines
822 B
Go

package middleware
import (
"fmt"
"net/http"
"time"
"github.com/Neur0toxine/sshpoke/internal/api/web/middleware/log"
"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())
}
}