ghttp.Server log功能完善

This commit is contained in:
John
2018-04-20 11:45:47 +08:00
parent 32bb5c79ef
commit 05741aadda
5 changed files with 26 additions and 15 deletions

View File

@ -5,6 +5,7 @@
// You can obtain one at https://gitee.com/johng/gf.
// Redis客户端.
// Redis中文手册文档请参考http://redisdoc.com/ Redis官方命令请参考https://redis.io/commands
package gredis
import (

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)
}

16
geg/net/ghttp/log.go Normal file
View File

@ -0,0 +1,16 @@
package main
import (
"gitee.com/johng/gf/g/net/ghttp"
)
func main() {
s := ghttp.GetServer()
s.BindHandler("/", func(r *ghttp.Request){
r.Response.Writeln("哈喽世界!")
})
s.SetAccessLogEnabled(true)
s.SetErrorLogEnabled(true)
s.SetPort(8199)
s.Run()
}