我们在项目的调试过程中,需要将一些自定义信息写入到log文件中,gin框架中提供了基础的Logger()方法,查看gin的文档,有如下信息:
How to write log file
func main() {
// Disable Console Color, you don't need console color when writing the logs to file.
gin.DisableConsoleColor()
// Logging to a file.
f, _ := os.Create("gin.log")
gin.DefaultWriter = io.MultiWriter(f)
// Use the following code if you need to write the logs to file and console at the same time.
// gin.DefaultWriter = io.MultiWriter(f, os.Stdout)
router := gin.Default()
router.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})
router.Run(":8080")
}
可以看到gin是通过DefaultWriter来确定日志写入的,查看代码(gin/mode.go中)如下:
// DefaultWriter is the default io.Writer used the Gin for debug output and
// middlewa