我有一堆路由,用gin.Default()
启动gin(默认情况下启用所有路由的日志记录和恢复)。但有一条路(即。/health
),每5秒ping一次。有什么简单的方法可以在不更改大量代码的情况下禁用该路由的请求日志记录?
func main() {
// gin is initialized upstream in our internal framework
// so I can't change this line easily.
router := gin.Default()
router.GET("/someGet", getting)
router.POST("/somePost", posting)
router.PUT("/somePut", putting)
router.DELETE("/someDelete", deleting)
// ... and more
// Disable request logging for only this route.
// Note: I'm hoping that there's some parameter I can pass in here to do that
route.GET("/health", health)
router.Run()
}
3条答案
按热度按时间icnyk63a1#
@Paul Lam的解决方案有效!以下代码供参考:
成为
从
github.com/gin-gonic/ [[email protected]](https://stackoverflow.com/cdn-cgi/l/email-protection) /gin.go
引用的gin.Default()
定义vc6uscn92#
gin.Logger()
有一个更长的形式,带有args,可以接受列出不应该记录的路径的字符串的args,即gin.LoggerWithWriter(gin.DefaultWriter, "<NO LOG PATH 1>", ...)
。我用
baseService.GinEngine = gin.New()
覆盖了基本结构体GinService,然后手动附加了gin.LoggerWithWriter(...)和gin.Recovery()中间件。这招奏效了
mmvthczy3#
还有一个
gin.LoggerWithConfig
中间件: