log.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package middleware
  2. import (
  3. "crawler/model"
  4. "crawler/utility"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. ua "github.com/mileusna/useragent"
  8. "time"
  9. )
  10. // 접속기록
  11. func Access() gin.HandlerFunc {
  12. return func(c *gin.Context) {
  13. var (
  14. gotParam gin.LogFormatterParams
  15. accessLog = new(model.AccessLogModel)
  16. start = time.Now()
  17. )
  18. c.Next()
  19. /*
  20. duration := utility.GetDurationInMillseconds(start)
  21. log.Printf("duration: %g\n", duration)
  22. */
  23. gin.LoggerWithConfig(gin.LoggerConfig{
  24. Formatter: func(param gin.LogFormatterParams) string {
  25. gotParam = param
  26. return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" %s\"\n",
  27. param.ClientIP,
  28. param.TimeStamp.Format(time.RFC1123),
  29. param.Method,
  30. param.Path,
  31. param.Request.Proto,
  32. param.StatusCode,
  33. param.Latency,
  34. param.Request.UserAgent(),
  35. param.ErrorMessage,
  36. )
  37. },
  38. })
  39. /*
  40. fmt.Printf("Request: %#v\n", gotParam.Request)
  41. fmt.Printf("TimeStamp: %s\n", gotParam.TimeStamp)
  42. fmt.Printf("StatusCode: %d\n", gotParam.StatusCode)
  43. fmt.Printf("Latency: %d\n", gotParam.Latency)
  44. fmt.Printf("ClientIP: %#v\n", gotParam.ClientIP)
  45. fmt.Printf("Method: %s\n", gotParam.Method)
  46. fmt.Printf("Path: %s\n", gotParam.Path)
  47. fmt.Printf("ErrorMessage: %s\n", gotParam.ErrorMessage)
  48. */
  49. var (
  50. requestID = c.Writer.Header().Get("Request-Id")
  51. status = c.Writer.Status() // access the status we are sending
  52. referer = c.Request.Referer()
  53. clientIP = utility.GetClientIP(c)
  54. method = c.Request.Method
  55. path = c.Request.RequestURI
  56. errorMessage = gotParam.ErrorMessage
  57. userAgent = c.Request.UserAgent()
  58. latency = time.Since(start).Seconds() // after request
  59. useragent = ua.Parse(userAgent)
  60. )
  61. /*
  62. fmt.Printf("RequestID: %#v\n", requestID)
  63. fmt.Printf("StatusCode: %d\n", statusCode)
  64. fmt.Printf("Referer: %s\n", referer)
  65. fmt.Printf("ClientIP: %#v\n", clientIP)
  66. fmt.Printf("Method: %s\n", method)
  67. fmt.Printf("Path: %s\n", path)
  68. fmt.Printf("ErrorMessage: %s\n", errorMessage)
  69. fmt.Printf("UserAgent: %s\n", userAgent)
  70. fmt.Printf("latency: %d\n", latency)
  71. fmt.Printf("status: %d\n", status)
  72. */
  73. accessLog.RequestID = requestID
  74. accessLog.RequestURI = path
  75. accessLog.ClientIP = clientIP
  76. accessLog.Referer = referer
  77. accessLog.UserAgent = userAgent
  78. accessLog.Browser = useragent.Name + " / " + useragent.Version
  79. accessLog.Os = useragent.OS + " / " + useragent.OSVersion
  80. accessLog.Device = useragent.Device
  81. accessLog.Method = method
  82. accessLog.ErrorMessage = errorMessage
  83. accessLog.Latency = fmt.Sprint(latency)
  84. accessLog.Status = status
  85. accessLog.Save()
  86. }
  87. }