nginx_access_log.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package po
  2. import (
  3. "fmt"
  4. "jollia.cn/golib/jokode"
  5. "strings"
  6. "time"
  7. )
  8. const tableNamePrefixNgAccLog = "ng_access_log"
  9. type NginxAccessLog struct {
  10. Id int64 `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
  11. RequestId string `json:"request_id" gorm:"column:request_id;type:varchar(255);not null;comment:请求ID"`
  12. TimeLocal time.Time `json:"time_local" gorm:"column:time_local;index;type:DATETIME;not null;comment:请求时间"`
  13. Host string `json:"host" gorm:"column:host;type:varchar(255);not null;comment:主机名"`
  14. RequestStr string `json:"request" gorm:"-"`
  15. Method string `json:"-" gorm:"column:method;type:varchar(30);not null;comment:Http请求方法"`
  16. Request string `json:"-" gorm:"column:request;type:varchar(2000);index;not null;comment;请求 Url"`
  17. HttpVer string `json:"-" gorm:"column:http_ver;type:VARCHAR(10);not null;comment:Http请求版本"`
  18. Status int `json:"status" gorm:"column:status;type:INT;not null;comment:响应状态码"`
  19. RemoteAddr string `json:"remote_addr" gorm:"column:remote_addr;type:VARCHAR(255);not null;comment:客户端IP地址"`
  20. Referer string `json:"referer,omitempty" gorm:"column:referer;type:VARCHAR(255);comment:来源页面"`
  21. Agent string `json:"agent,omitempty" gorm:"column:agent;type:TEXT;comment:用户代理(浏览器信息)"`
  22. XForwarded string `json:"x_forwarded,omitempty" gorm:"column:x_forwarded;type:VARCHAR(255);comment:X-Forwarded-For头域"`
  23. RequestLengthStr string `json:"request_length,omitempty" gorm:"-"`
  24. RequestLength int64 `json:"-" gorm:"column:request_length;type:BIGINT;comment:请求字节数"`
  25. BodyBytesSentStr string `json:"body_bytes_sent,omitempty" gorm:"-"`
  26. BodyBytesSent int64 `json:"-" gorm:"column:body_bytes_sent;type:BIGINT;comment:响应字节数"`
  27. UpAddr string `json:"up_addr,omitempty" gorm:"column:up_addr;type:VARCHAR(255);comment:上游服务器地址"`
  28. UpHost string `json:"up_host,omitempty" gorm:"column:up_host;type:VARCHAR(255);comment:上游服务器主机名"`
  29. UpRespTimeStr string `json:"up_resp_time,omitempty" gorm:"-"`
  30. UpRespTime float64 `json:"-" gorm:"column:up_resp_time;type:FLOAT;comment:上游服务器响应时间"`
  31. RequestTimeStr string `json:"request_time,omitempty" gorm:"-"`
  32. RequestTime float64 `json:"-" gorm:"column:request_time;type:FLOAT;comment:响应时间"`
  33. }
  34. //func (l *NginxAccessLog) TableName() string {
  35. // return tableNamePrefixNgAccLog
  36. //}
  37. func (l *NginxAccessLog) GetTableName() string {
  38. return fmt.Sprintf("%s_%s", tableNamePrefixNgAccLog, l.TimeLocal.Format("20060102"))
  39. }
  40. func (l *NginxAccessLog) FixFields() {
  41. if l.TimeLocal.IsZero() {
  42. l.TimeLocal = time.Now()
  43. }
  44. l.Method, l.Request, l.HttpVer = splitRequest(l.RequestStr)
  45. if l.RequestLengthStr != "" {
  46. l.RequestLength = jokode.AtoI64(l.RequestLengthStr)
  47. }
  48. if l.BodyBytesSentStr != "" {
  49. l.BodyBytesSent = jokode.AtoI64(l.BodyBytesSentStr)
  50. }
  51. if l.UpRespTimeStr != "" && l.UpRespTimeStr != "-" {
  52. l.UpRespTime = jokode.AtoF64(l.UpRespTimeStr)
  53. }
  54. if l.RequestTimeStr != "" && l.RequestTimeStr != "-" {
  55. l.RequestTime = jokode.AtoF64(l.RequestTimeStr)
  56. }
  57. }
  58. func splitRequest(r string) (method, url, ver string) {
  59. r = strings.TrimSpace(r)
  60. if leftIdx := strings.Index(r, " "); leftIdx != -1 {
  61. method = strings.TrimSpace(r[:leftIdx])
  62. if rightIdx := strings.LastIndex(r, " "); rightIdx != -1 {
  63. ver = r[rightIdx+1:]
  64. url = r[leftIdx+1 : rightIdx]
  65. } else {
  66. url = r[leftIdx+1:]
  67. }
  68. } else {
  69. url = r
  70. }
  71. return
  72. }