diff --git a/application.go b/application.go index 77126cb..c221de1 100644 --- a/application.go +++ b/application.go @@ -15,6 +15,7 @@ import ( "path/filepath" "strconv" "strings" + "time" ) type Application struct { @@ -281,6 +282,7 @@ func (that *Application) urlSer(url string) (string, []string) { //访问 func (that *Application) handler(w http.ResponseWriter, req *http.Request) { + nowUnixTime := time.Now() _, s := that.urlSer(req.RequestURI) //获取cookie @@ -326,10 +328,24 @@ func (that *Application) handler(w http.ResponseWriter, req *http.Request) { //跨域设置 that.crossDomain(&context) - //是否展示日志 - if that.WebConnectLog != nil { - that.WebConnectLog.Infoln(Substr(context.Req.RemoteAddr, 0, strings.Index(context.Req.RemoteAddr, ":")), context.Req.Method, context.HandlerStr) - } + + defer func() { + //是否展示日志 + if that.WebConnectLog != nil { + ipStr := Substr(context.Req.RemoteAddr, 0, strings.Index(context.Req.RemoteAddr, ":")) + //负载均衡优化 + if ipStr == "127.0.0.1" { + if req.Header.Get("X-Forwarded-For") != "" { + ipStr = req.Header.Get("X-Forwarded-For") + } else if req.Header.Get("X-Real-IP") != "" { + ipStr = req.Header.Get("X-Real-IP") + } + } + that.WebConnectLog.Infoln(ipStr, context.Req.Method, + "time cost:", ObjToFloat64(time.Now().UnixNano()-nowUnixTime.UnixNano())/1000000.00, "ms", + "data length:", ObjToFloat64(context.DataSize)/1000.00, "KB", context.HandlerStr) + } + }() //访问拦截true继续false暂停 connectListenerLen := len(that.connectListener) diff --git a/context.go b/context.go index 1a006a6..d85849e 100644 --- a/context.go +++ b/context.go @@ -18,6 +18,7 @@ type Context struct { RespData Map CacheIns SessionIns + DataSize int HandlerStr string //复写请求url } @@ -56,6 +57,7 @@ func (that *Context) Display(statu int, data interface{}) { } func (that *Context) View() { + if that.RespData == nil { return } @@ -63,6 +65,8 @@ func (that *Context) View() { if err != nil { return } + that.DataSize = len(d) that.RespData = nil that.Resp.Write(d) + return }