35 lines
693 B
Go
35 lines
693 B
Go
package middlewares
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func LoggingMiddleware() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
start := time.Now()
|
|
|
|
log.Info().
|
|
Str("type", "request").
|
|
Str("method", c.Request.Method).
|
|
Str("path", c.Request.URL.Path).
|
|
Str("ip", c.ClientIP()).
|
|
Str("query", c.Request.URL.RawQuery).
|
|
Time("time", start).
|
|
Msg("incoming request")
|
|
|
|
c.Next()
|
|
|
|
log.Info().
|
|
Str("type", "response").
|
|
Str("method", c.Request.Method).
|
|
Str("path", c.Request.URL.Path).
|
|
Int("status", c.Writer.Status()).
|
|
Dur("latency", time.Since(start)).
|
|
Time("time", time.Now()).
|
|
Msg("outgoing response")
|
|
}
|
|
}
|