package main import "github.com/gin-gonic/gin" func RequestLogger() gin.HandlerFunc { return func(c *gin.Context) { t := time.Now() c.Next() latency := time.Since(t) fmt.Printf("%s %s %s %s\n", c.Request.Method, c.Request.RequestURI, c.Request.Proto, latency, ) } } func ResponseLogger() gin.HandlerFunc { return func(c *gin.Context) { c.Writer.Header().Set("X-Content-Type-Options", "nosniff") c.Next() fmt.Printf("%d %s %s\n", c.Writer.Status(), c.Request.Method, c.Request.RequestURI, ) } } func main() { r := gin.Default() // Add logging middleware r.Use(RequestLogger()) r.Use(ResponseLogger()) r.GET("/", func(c *gin.Context) { c.JSON(200, gin.H{ "message": "Hello, World!", }) }) r.Run() // listen and serve on 0.0.0.0:8080 }