How to write log file

  1. func main() {
  2. // Disable Console Color, you don't need console color when writing the logs to file.
  3. gin.DisableConsoleColor()
  4. // Logging to a file.
  5. f, _ := os.Create("gin.log")
  6. gin.DefaultWriter = io.MultiWriter(f)
  7. // Use the following code if you need to write the logs to file and console at the same time.
  8. // gin.DefaultWriter = io.MultiWriter(f, os.Stdout)
  9. router := gin.Default()
  10. router.GET("/ping", func(c *gin.Context) {
  11. c.String(200, "pong")
  12. })
  13. router.Run(":8080")
  14. }