package logger import ( "fmt" "time" "github.com/gin-gonic/gin" "go.uber.org/zap" ) func Logger() gin.HandlerFunc { return func(c *gin.Context) { start := time.Now() path := c.Request.URL.Path raw := c.Request.URL.RawQuery // Process request c.Next() latency := time.Since(start) clientIP := c.ClientIP() method := c.Request.Method statusCode := c.Writer.Status() comment := c.Errors.ByType(gin.ErrorTypePrivate).String() if raw != "" { path = path + "?" + raw } fields := []zap.Field{ zap.Int("statusCode", statusCode), zap.Duration("latency", latency), zap.String("clientIP", clientIP), zap.String("method", method), zap.String("path", path), } if comment != "" { logger.Named("Gin").Error( comment, fields..., ) } else { logger.Named("Gin").Info(fmt.Sprintf("%s -> [%d] %s %s.", clientIP, statusCode, method, path), fields...) } } }