package api import ( "log" "time" "github.com/gin-gonic/gin" ) func requestLoggingMiddleware() gin.HandlerFunc { return func(c *gin.Context) { startedAt := time.Now() log.Printf( "request started: method=%s path=%s query=%s client_ip=%s", c.Request.Method, c.Request.URL.Path, c.Request.URL.RawQuery, c.ClientIP(), ) c.Next() finishedAt := time.Since(startedAt) if len(c.Errors) > 0 { log.Printf( "request finished with errors: method=%s path=%s status=%d latency=%s errors=%s", c.Request.Method, c.Request.URL.Path, c.Writer.Status(), finishedAt, c.Errors.String(), ) return } log.Printf( "request finished: method=%s path=%s status=%d latency=%s", c.Request.Method, c.Request.URL.Path, c.Writer.Status(), finishedAt, ) } }