ghttp.Server log功能完善

This commit is contained in:
John
2018-04-20 11:45:47 +08:00
parent 5fc727a119
commit 79ed4165ed
5 changed files with 26 additions and 15 deletions

View File

@ -13,9 +13,6 @@ func (s *Server) startCloseQueueLoop() {
for {
if v := s.closeQueue.PopFront(); v != nil {
r := v.(*Request)
s.handleAccessLog(r)
s.callHookHandler(r, "BeforeClose")
// 关闭当前会话的Cookie
r.Cookie.Close()

View File

@ -43,6 +43,7 @@ func (s *Server)handleRequest(w http.ResponseWriter, r *http.Request) {
if e := recover(); e != nil {
s.handleErrorLog(e, request)
}
s.handleAccessLog(request)
}()
// 事件 - BeforeServe
@ -134,7 +135,7 @@ func (s *Server)doServeFile(r *Request, path string) {
r.Response.WriteStatus(http.StatusForbidden)
}
} else {
// 读取文件内容返回
// 读取文件内容返回, no buffer
http.ServeContent(r.Response.ResponseWriter, &r.Request, info.Name(), info.ModTime(), f)
}
f.Close()

View File

@ -27,12 +27,9 @@ func (s *Server) handleAccessLog(r *Request) {
if v := r.Response.Header().Get("Status Code"); v != "" {
status = v
}
content := fmt.Sprintf(`%s %s %s`, r.Method, r.URL.String(), r.Proto)
content += fmt.Sprintf(", host: %s", r.Host)
content += fmt.Sprintf(", from: %s", strings.Split(r.RemoteAddr, ":")[0])
content += fmt.Sprintf(", refer: %s", r.Referer())
content += fmt.Sprintf(", status: %v", status)
s.logger.Info(content)
content := fmt.Sprintf(`"%s %s %s %s" %s`, r.Method, r.Host, r.URL.String(), r.Proto, status)
content += fmt.Sprintf(`, %s, "%s", "%s"`, strings.Split(r.RemoteAddr, ":")[0], r.Referer(), r.UserAgent())
s.logger.Println(content)
}
// 处理服务错误信息主要是panichttp请求的status由access log进行管理
@ -45,10 +42,9 @@ func (s *Server) handleErrorLog(error interface{}, r *Request) {
v(r, error)
return
}
content := fmt.Sprintf(`%s %s %s`, r.Method, r.URL.String(), r.Proto)
content += fmt.Sprintf(", host: %s", r.Host)
content += fmt.Sprintf(", from: %s", strings.Split(r.RemoteAddr, ":")[0])
content += fmt.Sprintf(", refer: %s", r.Referer())
content += fmt.Sprintf(", error: %v", error)
content := fmt.Sprintf(`"%s %s %s %s"`, r.Method, r.Host, r.URL.String(), r.Proto)
content += fmt.Sprintf(`, %s, "%s", "%s"`, strings.Split(r.RemoteAddr, ":")[0], r.Referer(), r.UserAgent())
content += fmt.Sprintf(`, %v`, error)
s.logger.Error(content)
}