如何关闭烦人的 [GIN-debug] 信息

如果你在使用 Gin 框架开发 Web 应用,你可能会在控制台看到很多以 [GIN-debug] 开头的调试信息,例如:

[GIN-debug] [WARNING] Running in 'debug' mode. Switch to 'release' mode in production.
- using env:   export GIN_MODE=release
- using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /ugreen/v1/docker/view/SwitchMirrorSource --> ugreen.git/library/ugreen/resp.Wrap.func1 (4 handlers)
[GIN-debug] GET    /ugreen/v1/docker/view/ObtainOverviewInfo --> ugreen.git/library/ugreen/resp.Wrap.func1 (4 handlers)
...

这些信息在开发过程中很有用,可以帮助你调试代码。但是,在生产环境中,这些信息可能会泄露敏感信息,并且会影响应用程序的性能。

好消息是,关闭这些调试信息非常简单!

方法一:设置环境变量

最简单的方法是设置环境变量 GIN_MODErelease

export GIN_MODE=release

方法二:在代码中设置

你也可以在代码中使用 gin.SetMode 函数来设置 Gin 的运行模式:

import (
	'github.com/gin-gonic/gin'
)

func main() {
	gin.SetMode(gin.ReleaseMode)

	// ... your code here
}

设置完毕后,重新启动你的应用程序,你就会发现那些烦人的 [GIN-debug] 信息不见了! 🎉

如何关闭Gin框架的调试信息 [GIN-debug]

原文地址: https://www.cveoy.top/t/topic/fP5q 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录