mirror of
https://gitee.com/johng/gf
synced 2026-06-07 10:22:11 +08:00
ghttp.Server 日志功能完善
This commit is contained in:
@ -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)
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user