Custom log file

For example:

  1. func main() {
  2. router := gin.New()
  3. // LoggerWithFormatter middleware will write the logs to gin.DefaultWriter
  4. // By default gin.DefaultWriter = os.Stdout
  5. router.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
  6. // your custom format
  7. return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" %s\"\n",
  8. param.ClientIP,
  9. param.TimeStamp.Format(time.RFC1123),
  10. param.Method,
  11. param.Path,
  12. param.Request.Proto,
  13. param.StatusCode,
  14. param.Latency,
  15. param.Request.UserAgent(),
  16. param.ErrorMessage,
  17. )
  18. }))
  19. router.Use(gin.Recovery())
  20. router.GET("/ping", func(c *gin.Context) {
  21. c.String(200, "pong")
  22. })
  23. router.Run(":8080")
  24. }

Sample Output

  1. ::1 - [Fri, 07 Dec 2018 17:04:38 JST] "GET /ping HTTP/1.1 200 122.767µs "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36" "

Last modified March 7, 2020 : add blog dir (#115) (f46734b)