|
@@ -0,0 +1,132 @@
|
|
|
+/*
|
|
|
+*
|
|
|
+日志过滤器
|
|
|
+记录网站所有访问和请求
|
|
|
+2015-7-2
|
|
|
+任政
|
|
|
+*
|
|
|
+*/
|
|
|
+package filter
|
|
|
+
|
|
|
+import (
|
|
|
+ "encoding/json"
|
|
|
+ "log"
|
|
|
+ "net/http"
|
|
|
+ "net/url"
|
|
|
+ "ossProxy/db"
|
|
|
+ "strings"
|
|
|
+ "sync"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ util "app.yhyue.com/moapp/jybase/common"
|
|
|
+
|
|
|
+ "app.yhyue.com/moapp/jybase/go-xweb/httpsession"
|
|
|
+)
|
|
|
+
|
|
|
+var (
|
|
|
+ arr = []map[string]interface{}{}
|
|
|
+ //对map的同步
|
|
|
+ lock = &sync.Mutex{}
|
|
|
+)
|
|
|
+
|
|
|
+// 日志过滤器
|
|
|
+type logFilter struct {
|
|
|
+ W http.ResponseWriter
|
|
|
+ R *http.Request
|
|
|
+ Session *httpsession.Session
|
|
|
+ //内存缓存日志map
|
|
|
+ GetSession map[string]interface{}
|
|
|
+ SetSession map[string]interface{}
|
|
|
+}
|
|
|
+
|
|
|
+// 继承过滤器方法
|
|
|
+func (l *logFilter) Do() bool {
|
|
|
+ go func() {
|
|
|
+ l.addLog()
|
|
|
+ if len(l.SetSession) > 0 {
|
|
|
+ l.Session.SetMultiple(l.SetSession)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
+// 定时保存日志
|
|
|
+func SaveLogTask() {
|
|
|
+ lock.Lock()
|
|
|
+ if len(arr) >= 1 {
|
|
|
+ tmp := arr
|
|
|
+ arr = make([]map[string]interface{}, 0)
|
|
|
+ go func() {
|
|
|
+ log.Println("timer..save..visit..log", len(tmp))
|
|
|
+ db.MG.DB("log").SaveBulk("jy_ossProxy_logs", tmp...)
|
|
|
+ }()
|
|
|
+ }
|
|
|
+
|
|
|
+ lock.Unlock()
|
|
|
+ time.AfterFunc(5*time.Minute, SaveLogTask)
|
|
|
+}
|
|
|
+
|
|
|
+func (l *logFilter) addLog() {
|
|
|
+ timeNow := time.Now()
|
|
|
+ agent := l.R.Header.Get("user-agent")
|
|
|
+ ref := l.R.Referer()
|
|
|
+ if l.GetSession["brefer"] == nil {
|
|
|
+ bexist := false
|
|
|
+ rurl, err := url.ParseRequestURI(ref)
|
|
|
+ if err == nil && rurl != nil {
|
|
|
+ host := rurl.Host
|
|
|
+ if host != "" {
|
|
|
+ host = strings.Split(host, ":")[0]
|
|
|
+ if strings.Index("127.0.0.1-www.jianyu360.com", host) < 0 && strings.Index(host, "qmx.top") < 0 {
|
|
|
+ bexist = true
|
|
|
+ l.SetSession["brefer"] = 1
|
|
|
+ l.SetSession["referhost"] = host
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if !bexist {
|
|
|
+ l.SetSession["brefer"] = -1
|
|
|
+ }
|
|
|
+ }
|
|
|
+ s_url := l.R.RequestURI
|
|
|
+ md, _ := json.Marshal(l.R.Form)
|
|
|
+ str := string(md)
|
|
|
+ var trustedId string
|
|
|
+ fidCookie, _ := l.R.Cookie("JYTrustedId")
|
|
|
+ if fidCookie != nil {
|
|
|
+ trustedId = fidCookie.Value
|
|
|
+ }
|
|
|
+ logs := map[string]interface{}{
|
|
|
+ "date": timeNow.Unix(),
|
|
|
+ "ip": util.GetIp(l.R),
|
|
|
+ "refer": ref,
|
|
|
+ "year": timeNow.Year(),
|
|
|
+ "month": timeNow.Month(),
|
|
|
+ "day": timeNow.Day(),
|
|
|
+ "hour": timeNow.Hour(),
|
|
|
+ "minutes": timeNow.Minute(),
|
|
|
+ "mdescribe": str,
|
|
|
+ "client": agent,
|
|
|
+ "os": util.GetOS(agent),
|
|
|
+ "browse": util.GetBrowse(agent),
|
|
|
+ "method": l.R.Method,
|
|
|
+ "url": s_url,
|
|
|
+ "trustedId": trustedId,
|
|
|
+ }
|
|
|
+ if l.GetSession["userId"] != nil {
|
|
|
+ logs["userid"] = l.GetSession["userId"]
|
|
|
+ logs["uname"] = l.GetSession["s_nickname"]
|
|
|
+ logs["positionid"] = l.GetSession["positionId"]
|
|
|
+ }
|
|
|
+ lock.Lock()
|
|
|
+ arr = append(arr, logs)
|
|
|
+ if len(arr) >= 500 || s_url == "/sl" {
|
|
|
+ tmp := arr
|
|
|
+ arr = make([]map[string]interface{}, 0)
|
|
|
+ go func() {
|
|
|
+ log.Println("save..visit..log", len(tmp))
|
|
|
+ db.MG.DB("log").SaveBulk("jy_ossProxy_logs", tmp...)
|
|
|
+ }()
|
|
|
+ }
|
|
|
+ lock.Unlock()
|
|
|
+}
|