ghttp.Request增加请求进入和完成时间记录,并增加到默认日志内容中

This commit is contained in:
John
2018-04-29 23:19:02 +08:00
parent bd7de6e522
commit 92800a1241
4 changed files with 25 additions and 10 deletions

View File

@ -13,6 +13,7 @@ import (
"gitee.com/johng/gf/g/encoding/gjson"
"gitee.com/johng/gf/g/container/gtype"
"gitee.com/johng/gf/g/util/gregx"
"gitee.com/johng/gf/g/os/gtime"
)
// 请求对象
@ -28,6 +29,8 @@ type Request struct {
Session *Session // 与当前请求绑定的Session对象(并发安全)
Response *Response // 对应请求的返回数据操作对象
Router *Router // 匹配到的路由对象
EnterTime int64 // 请求进入时间(微秒)
LeaveTime int64 // 请求完成时间(微秒)
}
// 创建一个Request对象
@ -43,6 +46,7 @@ func newRequest(s *Server, r *http.Request, w http.ResponseWriter) *Request {
Response : &Response {
ResponseWriter : ResponseWriter{w, http.StatusOK, 0},
},
EnterTime : gtime.Microsecond(),
}
// 会话处理
request.Cookie = GetCookie(request)

View File

@ -17,6 +17,7 @@ import (
"net/http"
"gitee.com/johng/gf/g/os/gfile"
"gitee.com/johng/gf/g/encoding/ghtml"
"gitee.com/johng/gf/g/os/gtime"
)
// 默认HTTP Server处理入口http包底层默认使用了gorutine异步处理请求所以这里不再异步执行
@ -39,6 +40,9 @@ func (s *Server)handleRequest(w http.ResponseWriter, r *http.Request) {
// 错误日志使用recover进行判断
defer func() {
if request.LeaveTime == 0 {
request.LeaveTime = gtime.Microsecond()
}
if e := recover(); e != nil {
s.handleErrorLog(e, request)
}
@ -54,6 +58,8 @@ func (s *Server)handleRequest(w http.ResponseWriter, r *http.Request) {
}
// 事件 - AfterServe
s.callHookHandler(request, "AfterServe")
// 设置请求完成时间
request.LeaveTime = gtime.Microsecond()
// 事件 - BeforeOutput
s.callHookHandler(request, "BeforeOutput")
// 输出Cookie

View File

@ -23,7 +23,12 @@ func (s *Server) handleAccessLog(r *Request) {
v(r)
return
}
content := fmt.Sprintf(`"%s %s %s %s" %s %s`, r.Method, r.Host, r.URL.String(), r.Proto, gconv.String(r.Response.Status), gconv.String(r.Response.Length))
content := fmt.Sprintf(`"%s %s %s %s" %s %s`,
r.Method, r.Host, r.URL.String(), r.Proto,
gconv.String(r.Response.Status),
gconv.String(r.Response.Length),
)
content += fmt.Sprintf(` %.3f`, float64(r.LeaveTime - r.EnterTime)/1000)
content += fmt.Sprintf(`, %s, "%s", "%s"`, r.GetClientIp(), r.Referer(), r.UserAgent())
s.accessLogger.Println(content)
}
@ -42,6 +47,7 @@ func (s *Server) handleErrorLog(error interface{}, r *Request) {
}
content := fmt.Sprintf(`%v, "%s %s %s %s"`, error, r.Method, r.Host, r.URL.String(), r.Proto)
content += fmt.Sprintf(` %.3f`, float64(r.LeaveTime - r.EnterTime)/1000)
content += fmt.Sprintf(`, %s, "%s", "%s"`, r.GetClientIp(), r.Referer(), r.UserAgent())
s.errorLogger.Error(content)
}

View File

@ -1,7 +1,6 @@
package main
import (
"fmt"
"net/http"
"gitee.com/johng/gf/g/net/ghttp"
)
@ -13,14 +12,14 @@ func main() {
})
s.SetAccessLogEnabled(true)
s.SetErrorLogEnabled(true)
s.SetLogHandler(func(r *ghttp.Request, error ...interface{}) {
if len(error) > 0 {
// 如果是错误日志
fmt.Println("错误产生了:", error[0])
}
// 这里是请求日志
fmt.Println("请求处理完成,请求地址:", r.URL.String(), "请求结果:", r.Response.Status)
})
//s.SetLogHandler(func(r *ghttp.Request, error ...interface{}) {
// if len(error) > 0 {
// // 如果是错误日志
// fmt.Println("错误产生了:", error[0])
// }
// // 这里是请求日志
// fmt.Println("请求处理完成,请求地址:", r.URL.String(), "请求结果:", r.Response.Status)
//})
s.SetPort(8199)
s.Run()
}