mgosl.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. *中台api mongodb 保存请求日志
  3. *
  4. */
  5. package common
  6. import (
  7. "app.yhyue.com/moapp/jybase/mongodb"
  8. "fmt"
  9. "io/ioutil"
  10. "log"
  11. "net/http"
  12. "regexp"
  13. "sync"
  14. "time"
  15. )
  16. var (
  17. arr = map[string][]map[string]interface{}{}
  18. //对map的同步
  19. saveLogsLock = &sync.Mutex{}
  20. saveCount = map[string]int{}
  21. initCount = 500
  22. )
  23. //添加日志
  24. func AddMgoLogs(mgoLog mongodb.MongodbSim, r *http.Request, tableName string, ExcludeUrl []*regexp.Regexp, c int) {
  25. for _, v := range ExcludeUrl {
  26. // 过滤无意义路由日志
  27. if v.MatchString(r.URL.Path) {
  28. return
  29. }
  30. }
  31. if saveCount[tableName] == 0 {
  32. c = If(c > 0, c, initCount).(int)
  33. saveCount[tableName] = c
  34. go SaveLogTask(mgoLog, tableName)
  35. }
  36. timeNow := time.Now()
  37. agent := r.Header.Get("user-agent")
  38. ref := r.Referer()
  39. s_url := r.RequestURI
  40. md, _ := ioutil.ReadAll(r.Body)
  41. //md, _ := json.Marshal(r.Form)
  42. str := string(md)
  43. logs := map[string]interface{}{
  44. "date": timeNow.Unix(),
  45. "ip": GetIp(r),
  46. "refer": ref,
  47. "year": timeNow.Year(),
  48. "month": timeNow.Month(),
  49. "day": timeNow.Day(),
  50. "hour": timeNow.Hour(),
  51. "minutes": timeNow.Minute(),
  52. "mdescribe": str,
  53. "client": agent,
  54. "os": GetOS(agent),
  55. "browse": GetBrowse(agent),
  56. "method": r.Method,
  57. "url": s_url,
  58. "userid": r.Header.Get("userId"),
  59. }
  60. saveLogsLock.Lock()
  61. arr[tableName] = append(arr[tableName], logs)
  62. if len(arr[tableName]) >= saveCount[tableName] {
  63. tmp := arr[tableName]
  64. arr[tableName] = make([]map[string]interface{}, 0)
  65. go func(tableName string, tmp []map[string]interface{}) {
  66. log.Println(fmt.Sprintf("%s save..visit..log %d", tableName, len(tmp)))
  67. mgoLog.SaveBulk(tableName, tmp...)
  68. }(tableName, tmp)
  69. }
  70. saveLogsLock.Unlock()
  71. }
  72. //定时保存
  73. //定时保存日志
  74. func SaveLogTask(mgoLog mongodb.MongodbSim, tableName string) {
  75. CrontabByTicker(false, 5, func() {
  76. lock.Lock()
  77. if len(arr[tableName]) >= 1 {
  78. tmp := arr[tableName]
  79. arr[tableName] = make([]map[string]interface{}, 0)
  80. go func() {
  81. log.Println(fmt.Sprintf("ticker: %s save..visit..log %d", tableName, len(tmp)))
  82. mgoLog.SaveBulk(tableName, tmp...)
  83. }()
  84. }
  85. lock.Unlock()
  86. })
  87. }
  88. //定时任务
  89. func CrontabByTicker(flag bool, c int, f func()) {
  90. if c == 0 {
  91. log.Println("定时任务参数有误:", c)
  92. return
  93. }
  94. //默认5分钟
  95. countdown := 5 * 60
  96. if c > 0 {
  97. countdown = c * 60
  98. }
  99. if flag {
  100. go f()
  101. }
  102. ticker := time.NewTicker(time.Duration(countdown) * time.Second)
  103. go func(t *time.Ticker) {
  104. for {
  105. select {
  106. case <-t.C:
  107. go f()
  108. }
  109. }
  110. }(ticker)
  111. }