accessLog.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package model
  2. import (
  3. "crawler/config"
  4. "crawler/service"
  5. )
  6. type AccessLogInterface interface {
  7. Save()
  8. }
  9. type AccessLogModel struct {
  10. AccessLog
  11. }
  12. type AccessLog struct {
  13. RequestID string `json:"request_id"`
  14. RequestURI string `json:"request_uri"`
  15. ClientIP string `json:"client_ip"`
  16. Referer string `json:"referer"`
  17. UserAgent string `json:"useragent"`
  18. Browser string `json:"browser"`
  19. Os string `json:"os"`
  20. Device string `json:"device"`
  21. Method string `json:"method"`
  22. ErrorMessage string `json:"error_message"`
  23. Latency string `json:"latency"`
  24. Status int `json:"status"`
  25. CreatedAt string `json:"created_at"`
  26. }
  27. // 접속기록
  28. func (this *AccessLogModel) Save() {
  29. var (
  30. db = service.DB_CRAWLER
  31. conn = db.SQLDB
  32. )
  33. sql := `
  34. INSERT INTO tb_access_log
  35. SET
  36. request_id = ?,
  37. request_uri = ?,
  38. client_ip = ?,
  39. referer = ?,
  40. useragent = ?,
  41. browser = ?,
  42. os = ?,
  43. device = ?,
  44. method = ?,
  45. error_message = ?,
  46. latency = ?,
  47. status = ?,
  48. created_at = NOW();
  49. `
  50. _, err := conn.Exec(sql,
  51. this.RequestID, this.RequestURI, this.ClientIP, this.Referer,
  52. this.UserAgent, this.Browser, this.Os, this.Device, this.Method,
  53. this.ErrorMessage, this.Latency, this.Status,
  54. )
  55. if err != nil {
  56. db.SetErrorLog(err, sql)
  57. }
  58. db.SetGeneralLog(config.GL_ACTION_WRITE, sql, "insert access log")
  59. }