123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- /**
- 日志过滤器
- 记录网站所有访问和请求
- 2015-7-2
- 任政
- **/
- package filter
- import (
- util "app.yhyue.com/moapp/jybase/common"
- "encoding/json"
- "jy-docs/public"
- "log"
- "net/http"
- "net/url"
- "strings"
- "sync"
- "time"
- "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))
- public.Mgo_Log.SaveBulk("jy_docs_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)
- 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,
- }
- if l.GetSession["userId"] != nil {
- logs["userid"] = l.GetSession["userId"]
- logs["uname"] = l.GetSession["s_nickname"]
- }
- 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))
- public.Mgo_Log.SaveBulk("jy_docs_logs", tmp...)
- }()
- }
- lock.Unlock()
- }
|