wangshan %!s(int64=7) %!d(string=hai) anos
pai
achega
9d98abf652
Modificáronse 2 ficheiros con 52 adicións e 23 borrados
  1. 52 3
      src/apiservice/oamanager/content.go
  2. 0 20
      src/apiservice/oamanager/oamanager.go

+ 52 - 3
src/apiservice/oamanager/content.go

@@ -1,6 +1,7 @@
 package oamanager
 
 import (
+	"log"
 	"net"
 	"net/http"
 	"qfw/util"
@@ -8,7 +9,7 @@ import (
 	"qfw/util/redis"
 	"regexp"
 	"strings"
-
+	"sync"
 	"time"
 
 	"github.com/go-xweb/xweb"
@@ -21,6 +22,16 @@ type Content struct {
 
 var regex = regexp.MustCompile("(Android|Mobile)")
 
+//对map的同步
+var lock sync.Mutex
+
+//内存缓存日志数量,超过此数量存库
+var nc = 500
+
+//内存缓存日志map
+var arr = make([]map[string]interface{}, 0)
+var arrapi = make([]map[string]interface{}, 0)
+
 func init() {
 	xweb.AddAction(&Content{})
 }
@@ -29,7 +40,7 @@ func (c *Content) Article(id string) {
 	client := c.Header("User-Agent")
 	c.Request.Proto = GetIp(c.Request) //ip
 	var data map[string]interface{}
-	go addLog(c.Request)
+	addLog(c.Request)
 	bm := false //访问日志
 	bm = regex.MatchString(client)
 	sid_openid := util.DecodeArticleId2ByCheck(id)[0] //解密id
@@ -76,7 +87,45 @@ func addLog(req *http.Request) {
 		"s_method":   req.Method,
 		"s_url":      s_url,
 	}
-	mongodb.Save("log", logs)
+	lock.Lock()
+	arr = append(arr, logs)
+	if len(arr) >= nc {
+		tmp := arr
+		arr = make([]map[string]interface{}, 0)
+		go func() {
+			log.Println("save..article..log", len(tmp))
+			mongodb.SaveBulk("log", tmp...)
+		}()
+	}
+	lock.Unlock()
+}
+
+func APIlog(username, action, keyword, appid, rMsg, signature string, pagenum, timestamp, rCode, apicount int) {
+	defer util.Catch()
+	logs := map[string]interface{}{
+		"s_username":  username,
+		"s_keyword":   keyword,
+		"s_appid":     appid,
+		"s_rMsg":      rMsg,
+		"s_signature": signature,
+		"s_action":    action,
+		"i_pagenum":   pagenum,
+		"i_timestamp": timestamp,
+		"i_rCode":     rCode,
+		"i_apicount":  apicount,
+		"l_date":      time.Now().Unix(),
+	}
+	lock.Lock()
+	arrapi = append(arrapi, logs)
+	if len(arrapi) >= nc {
+		tmp := arrapi
+		arrapi = make([]map[string]interface{}, 0)
+		go func() {
+			log.Println("save..article..log", len(tmp))
+			mongodb.SaveBulk("apilog", tmp...)
+		}()
+	}
+	lock.Unlock()
 }
 
 //获取平台类型

+ 0 - 20
src/apiservice/oamanager/oamanager.go

@@ -177,26 +177,6 @@ func OpenAction(w http.ResponseWriter, r *http.Request) {
 		w.Write([]byte(fmt.Sprintf("{\"code\":%d,\"msg\":\"%s\"}", rCode, rMsg)))
 	}
 }
-func APIlog(username, action, keyword, appid, rMsg, signature string, pagenum, timestamp, rCode, apicount int) {
-	defer util.Catch()
-	logs := map[string]interface{}{
-		"s_username":  username,
-		"s_keyword":   keyword,
-		"s_appid":     appid,
-		"s_rMsg":      rMsg,
-		"s_signature": signature,
-		"s_action":    action,
-		"i_pagenum":   pagenum,
-		"i_timestamp": timestamp,
-		"i_rCode":     rCode,
-		"i_apicount":  apicount,
-		"l_date":      time.Now().Unix(),
-	}
-	id := mongodb.Save("apilog", logs)
-	if len(id) < 1 {
-		log.Println("保存调用接口日志失败")
-	}
-}
 
 //
 func GetData(pagenum int, keyword string, ordernum int) (list []map[string]interface{}) {