...

Source file src/github.com/gin-gonic/contrib/ginrus/example/example.go

Documentation: github.com/gin-gonic/contrib/ginrus/example

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"time"
     7  
     8  	"github.com/gin-gonic/contrib/ginrus"
     9  	"github.com/gin-gonic/gin"
    10  	"github.com/sirupsen/logrus"
    11  )
    12  
    13  func main() {
    14  	r := gin.New()
    15  
    16  	// Add a ginrus middleware, which:
    17  	//   - Logs all requests, like a combined access and error log.
    18  	//   - Logs to stdout.
    19  	//   - RFC3339 with UTC time format.
    20  	r.Use(ginrus.Ginrus(logrus.StandardLogger(), time.RFC3339, true))
    21  
    22  	// Add similar middleware, but:
    23  	//   - Only logs requests with errors, like an error log.
    24  	//   - Logs to stderr instead of stdout.
    25  	//   - Local time zone instead of UTC.
    26  	logger := logrus.New()
    27  	logger.Level = logrus.ErrorLevel
    28  	logger.Out = os.Stderr
    29  	r.Use(ginrus.Ginrus(logger, time.RFC3339, false))
    30  
    31  	// Example ping request.
    32  	r.GET("/ping", func(c *gin.Context) {
    33  		c.String(200, "pong "+fmt.Sprint(time.Now().Unix()))
    34  	})
    35  
    36  	// Listen and Server in 0.0.0.0:8080
    37  	r.Run(":8080")
    38  }
    39  

View as plain text