mirror of
https://gitee.com/johng/gf
synced 2026-06-27 01:43:33 +08:00
ghttp.Server log功能完善
This commit is contained in:
@ -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 (
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -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()
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
// 处理服务错误信息,主要是panic,http请求的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
16
geg/net/ghttp/log.go
Normal 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()
|
||||
}
|
||||
Reference in New Issue
Block a user