package middleware

import (
	"log"
	"time"

	"github.com/gin-gonic/gin"
)

// GinLogger returns a middleware that logs requests using the standard logger
func GinLogger() gin.HandlerFunc {
	return func(c *gin.Context) {
		start := time.Now()
		c.Next()
		latency := time.Since(start)
		status := c.Writer.Status()
		if len(c.Errors) > 0 {
			log.Printf("error: method=%s path=%s status=%d latency=%s errs=%s", c.Request.Method, c.Request.URL.Path, status, latency.String(), c.Errors.String())
			return
		}
		log.Printf("method=%s path=%s status=%d latency=%s", c.Request.Method, c.Request.URL.Path, status, latency.String())
	}
}
