zoukankan      html  css  js  c++  java
  • 自定义路由日志的格式

    默认的路由日志是这样的:

    [GIN-debug] POST   /foo                      --> main.main.func1 (3 handlers)
    [GIN-debug] GET    /bar                      --> main.main.func2 (3 handlers)
    [GIN-debug] GET    /status                   --> main.main.func3 (3 handlers)
    

    如果你想以给定的格式记录这些信息(例如 JSON,键值对或其他格式),你可以使用gin.DebugPrintRouteFunc来定义格式,在下面的示例中,我们使用标准日志包记录路由日志,你可以使用其他适合你需求的日志工具

    import (
    	"log"
    	"net/http"
    
    	"github.com/gin-gonic/gin"
    )
    
    func main() {
    	r := gin.Default()
    	gin.DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {
    		log.Printf("endpoint %v %v %v %v
    ", httpMethod, absolutePath, handlerName, nuHandlers)
    	}
    
    	r.POST("/foo", func(c *gin.Context) {
    		c.JSON(http.StatusOK, "foo")
    	})
    
    	r.GET("/bar", func(c *gin.Context) {
    		c.JSON(http.StatusOK, "bar")
    	})
    
    	r.GET("/status", func(c *gin.Context) {
    		c.JSON(http.StatusOK, "ok")
    	})
    
    	// Listen and Server in http://0.0.0.0:8080
    	r.Run()
    }
    

      

  • 相关阅读:
    String类型操作命令及api使用
    回顾Redis基础
    kibana6 安装
    elasticsearch安装
    flink 异常
    Scala异常
    idea配置
    mysql error 1577解决
    Hbase与phoenix关联
    CS61b lab4打卡
  • 原文地址:https://www.cnblogs.com/yzg-14/p/13153443.html
Copyright © 2011-2022 走看看