From 1c29ae66bedab4ecb60838f2c715f4d81146042e Mon Sep 17 00:00:00 2001 From: John Date: Fri, 20 Apr 2018 17:53:34 +0800 Subject: [PATCH] =?UTF-8?q?ghttp.Server=20=E6=97=A5=E5=BF=97=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/net/ghttp/http_server.go | 1 + g/net/ghttp/http_server_log.go | 3 +++ g/os/glog/glog.go | 16 +++++++++------- g/os/glog/glog_logger.go | 11 +++++++++-- geg/net/ghttp/log.go | 1 + 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/g/net/ghttp/http_server.go b/g/net/ghttp/http_server.go index 1516ced8c..6fccbf003 100644 --- a/g/net/ghttp/http_server.go +++ b/g/net/ghttp/http_server.go @@ -111,6 +111,7 @@ func GetServer(names...string) (*Server) { logger : glog.New(), logHandler : gtype.NewInterface(), } + s.logger.SetBacktraceSkip(4) // 设置路由解析缓存上限,使用LRU进行缓存淘汰 s.hooksCache.SetCap(10000) s.handlerCache.SetCap(10000) diff --git a/g/net/ghttp/http_server_log.go b/g/net/ghttp/http_server_log.go index 57384c4ab..6d1fdc20a 100644 --- a/g/net/ghttp/http_server_log.go +++ b/g/net/ghttp/http_server_log.go @@ -10,6 +10,7 @@ package ghttp import ( "fmt" "gitee.com/johng/gf/g/util/gconv" + "net/http" ) // 处理服务错误信息,主要是panic,http请求的status由access log进行管理 @@ -29,6 +30,8 @@ func (s *Server) handleAccessLog(r *Request) { // 处理服务错误信息,主要是panic,http请求的status由access log进行管理 func (s *Server) handleErrorLog(error interface{}, r *Request) { + r.Response.WriteStatus(http.StatusInternalServerError) + if !s.IsErrorLogEnabled() { return } diff --git a/g/os/glog/glog.go b/g/os/glog/glog.go index eb8cd3118..00f825d81 100644 --- a/g/os/glog/glog.go +++ b/g/os/glog/glog.go @@ -15,10 +15,11 @@ import ( ) type Logger struct { - mu sync.RWMutex - io io.Writer // 日志内容写入的IO接口 - path *gtype.String // 日志写入的目录路径 - debug *gtype.Bool // 是否允许输出DEBUG信息 + mu sync.RWMutex + io io.Writer // 日志内容写入的IO接口 + path *gtype.String // 日志写入的目录路径 + debug *gtype.Bool // 是否允许输出DEBUG信息 + btSkip *gtype.Int // 错误产生时的backtrace回调信息skip条数 } // 默认的日志对象 @@ -27,9 +28,10 @@ var logger = New() // 新建自定义的日志操作对象 func New() *Logger { return &Logger { - io : nil, - path : gtype.NewString(), - debug : gtype.NewBool(true), + io : nil, + path : gtype.NewString(), + debug : gtype.NewBool(true), + btSkip : gtype.NewInt(3), } } diff --git a/g/os/glog/glog_logger.go b/g/os/glog/glog_logger.go index 58888cc0a..adcd81f8f 100644 --- a/g/os/glog/glog_logger.go +++ b/g/os/glog/glog_logger.go @@ -24,6 +24,11 @@ const ( gDEFAULT_FILE_POOL_FLAGS = os.O_CREATE|os.O_WRONLY|os.O_APPEND ) +// 设置BacktraceSkip +func (l *Logger) SetBacktraceSkip(skip int) { + l.btSkip.Set(skip) +} + // 可自定义IO接口 func (l *Logger) SetIO(w io.Writer) { l.mu.RLock() @@ -114,11 +119,13 @@ func (l *Logger) errPrint(s string) { // 调用回溯字符串 func (l *Logger) backtrace() string { backtrace := "Trace:\n" + index := 1 for i := 1; i < 10000; i++ { - if _, cfile, cline, ok := runtime.Caller(i + 3); ok { + if _, cfile, cline, ok := runtime.Caller(i + l.btSkip.Val()); ok { // 不打印出go源码路径 if !gregx.IsMatchString("^" + runtime.GOROOT(), cfile) { - backtrace += strconv.Itoa(i) + ". " + cfile + ":" + strconv.Itoa(cline) + "\n" + backtrace += strconv.Itoa(index) + ". " + cfile + ":" + strconv.Itoa(cline) + "\n" + index++ } } else { break diff --git a/geg/net/ghttp/log.go b/geg/net/ghttp/log.go index ecde279a4..69c789f76 100644 --- a/geg/net/ghttp/log.go +++ b/geg/net/ghttp/log.go @@ -8,6 +8,7 @@ func main() { s := ghttp.GetServer() s.BindHandler("/", func(r *ghttp.Request){ r.Response.Writeln("哈喽世界!") + panic("test") }) s.SetAccessLogEnabled(true) s.SetErrorLogEnabled(true)