1. 程式人生 > >gin框架中自定義向log中寫入極速快三源碼搭建調試信息

gin框架中自定義向log中寫入極速快三源碼搭建調試信息

configure .get owin 基礎 stat lora 定義 通過 coloring

我們在極速快三源碼搭建項目的調試過程中,需要將一些自定義信息寫入到log文件中,gin框架中提供了基礎的Logger()方法,查看gin的文檔,有如下信息:企 娥:217 1793 408

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
// middleware output like Logger() or Recovery().
// Note that both Logger and Recovery provides custom ways to configure their
// output io.Writer.
// To support coloring in Windows use:

// import "/mattn/go-colorable"
// gin.DefaultWriter = colorable.NewColorableStdout()
var DefaultWriter io.Writer = os.Stdout
var DefaultErrorWriter io.Writer = os.Stderr
DefaultWriter是包外可見的變量,類型是io.Writer,在gin自己的Logger中調用方法如下:

fmt.Fprintf(out, "[GIN] %v |%s %3d %s| %13v | %15s |%s %-7s %s %s\n%s",

end.Format("2006/01/02 - 15:04:05"),
statusColor, statusCode, resetColor,
latency,
clientIP,
methodColor, method, resetColor,
path,
comment,
)

gin框架中自定義向log中寫入極速快三源碼搭建調試信息