1、在goland编辑器中,查看日志信息时,全是灰色的日志颜色,查找问题不好定位,就想着在服务器控制台查看请求日志时,不同的问题显示不同颜色日志信息,这样便于查找问题,查看了gin框架源码,有一个函数直接在main里面调用就可以了。
在main.go里调用:gin.ForceConsoleColor()
package main
import (
"github.com/gin-gonic/gin"
"test.cn/src/utils"
)
func main() {
//直接在这里调用这一个函数就可以了
gin.ForceConsoleColor()
r := gin.Default()
r.GET("/info", func(c *gin.Context) {
cpuInfo := utils.GetCpuPercent()
memInfo:=utils.GetMemPercent()
diskInfo:=utils.GetDiskPercent()
hostInfo:=utils.GetHostPercent()
c.JSON(200, gin.H{
"cpuInfo": cpuInfo,
"memInfo": memInfo,
"diskInfo": diskInfo,
"hostInfo": hostInfo,
})
})
r.Run(":8008") // listen and serve on 0.0.0.0:8080
}