From a3aa5e7cd7d30c6289028323cb95251bc1ce2f03 Mon Sep 17 00:00:00 2001 From: John Date: Mon, 27 Aug 2018 23:58:32 +0800 Subject: [PATCH] =?UTF-8?q?gdb=E5=A2=9E=E5=8A=A0=E8=B0=83=E8=AF=95?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E7=9A=84=E8=87=AA=E5=8A=A8=E8=B0=83=E8=AF=95?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E8=BE=93=E5=87=BA=EF=BC=8C=E5=BC=80=E5=8F=91?= =?UTF-8?q?=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TODO | 4 ++-- g/database/gdb/gdb_base.go | 29 +++++++++++++++++++++++++---- g/database/gdb/gdb_transaction.go | 12 ++++++++---- g/net/ghttp/ghttp_server.go | 4 ++-- g/os/glog/glog.go | 20 +++++++++++++++++++- g/os/glog/glog_logger.go | 25 +++++++++++++++++++------ geg/database/orm/mysql/gdb_debug.go | 4 ++-- geg/os/glog/glog_backtrace.go | 17 +++++++++++++++++ 8 files changed, 94 insertions(+), 21 deletions(-) create mode 100644 geg/os/glog/glog_backtrace.go diff --git a/TODO b/TODO index e86f7627e..1f60f6537 100644 --- a/TODO +++ b/TODO @@ -11,7 +11,6 @@ Cookie&Session数据池化处理; ghttp.Client增加proxy特性; gtime增加对时区转换的封装,并简化失去转换时对类似+80500时区的支持; orm增加sqlite对Save方法的支持(去掉触发器语句); -ghttp.Server的Cookie及Session锁机制优化(去掉map锁机制); ghttp.Server增加Ip访问控制功能(DenyIps&AllowIps); ghttp路由功能增加分组路由特性; 解决glog串日志情况; @@ -63,4 +62,5 @@ DONE: 36. gcache检查在i386下的int64->int转换问题; 37. ghttp获取参数支持直接转struct功能; 38. gfsnotify增加对于目录的监控; -39. 检查windows下的平滑重启失效问题; \ No newline at end of file +39. 检查windows下的平滑重启失效问题; +40. ghttp.Server的Cookie及Session锁机制优化(去掉map锁机制); \ No newline at end of file diff --git a/g/database/gdb/gdb_base.go b/g/database/gdb/gdb_base.go index 7270e4c60..5feeab3f3 100644 --- a/g/database/gdb/gdb_base.go +++ b/g/database/gdb/gdb_base.go @@ -18,6 +18,7 @@ import ( "gitee.com/johng/gf/g/container/gring" "gitee.com/johng/gf/g/os/gtime" "time" + "gitee.com/johng/gf/g/os/glog" ) const ( @@ -64,6 +65,22 @@ func (db *Db) PrintQueriedSqls() { } } +// 打印SQL对象(仅在debug=true时有效) +func (db *Db) printSql(v *Sql) { + s := fmt.Sprintf("%s, %v, %s, %s, %d ms, %s", v.Sql, v.Args, + gtime.NewFromTimeStamp(v.Start).Format("Y-m-d H:i:s.u"), + gtime.NewFromTimeStamp(v.End).Format("Y-m-d H:i:s.u"), + v.End - v.Start, v.Func, + ) + if v.Error != nil { + s += "\nError: " + v.Error.Error() + glog.Error(s) + } else { + glog.Debug(s) + } + +} + // 关闭链接 func (db *Db) Close() error { if db.master != nil { @@ -92,14 +109,16 @@ func (db *Db) Query(query string, args ...interface{}) (*sql.Rows, error) { militime1 := gtime.Millisecond() rows, err = db.slave.Query(*p, args ...) militime2 := gtime.Millisecond() - db.sqls.Put(&Sql{ + s := &Sql{ Sql : *p, Args : args, Error : err, Start : militime1, End : militime2, Func : "DB:Query", - }) + } + db.sqls.Put(s) + db.printSql(s) } else { rows, err = db.slave.Query(*p, args ...) } @@ -120,14 +139,16 @@ func (db *Db) Exec(query string, args ...interface{}) (sql.Result, error) { militime1 := gtime.Millisecond() result, err = db.master.Exec(*p, args ...) militime2 := gtime.Millisecond() - db.sqls.Put(&Sql{ + s := &Sql{ Sql : *p, Args : args, Error : err, Start : militime1, End : militime2, Func : "DB:Exec", - }) + } + db.sqls.Put(s) + db.printSql(s) } else { result, err = db.master.Exec(*p, args ...) } diff --git a/g/database/gdb/gdb_transaction.go b/g/database/gdb/gdb_transaction.go index 86d51d7dd..e911ce684 100644 --- a/g/database/gdb/gdb_transaction.go +++ b/g/database/gdb/gdb_transaction.go @@ -42,14 +42,16 @@ func (tx *Tx) Query(query string, args ...interface{}) (*sql.Rows, error) { militime1 := gtime.Millisecond() rows, err = tx.tx.Query(*p, args ...) militime2 := gtime.Millisecond() - tx.db.sqls.Put(&Sql{ + s := &Sql{ Sql : *p, Args : args, Error : err, Start : militime1, End : militime2, Func : "TX:Query", - }) + } + tx.db.sqls.Put(s) + tx.db.printSql(s) } else { rows, err = tx.tx.Query(*p, args ...) } @@ -70,14 +72,16 @@ func (tx *Tx) Exec(query string, args ...interface{}) (sql.Result, error) { militime1 := gtime.Millisecond() result, err = tx.tx.Exec(*p, args ...) militime2 := gtime.Millisecond() - tx.db.sqls.Put(&Sql{ + s := &Sql{ Sql : *p, Args : args, Error : err, Start : militime1, End : militime2, Func : "TX:Exec", - }) + } + tx.db.sqls.Put(s) + tx.db.printSql(s) } else { result, err = tx.tx.Exec(*p, args ...) } diff --git a/g/net/ghttp/ghttp_server.go b/g/net/ghttp/ghttp_server.go index db92fd7ef..b2052fadc 100644 --- a/g/net/ghttp/ghttp_server.go +++ b/g/net/ghttp/ghttp_server.go @@ -204,8 +204,8 @@ func GetServer(name...interface{}) (*Server) { nameToUriType : gtype.NewInt(), gzipMimesMap : make(map[string]struct{}), } - s.errorLogger.SetBacktraceSkip(4) - s.accessLogger.SetBacktraceSkip(4) + s.errorLogger.SetBacktraceSkip(1) + s.accessLogger.SetBacktraceSkip(1) // 设置路由解析缓存上限,使用LRU进行缓存淘汰 s.serveCache.SetCap(gSERVE_CACHE_LRU_SIZE) s.hooksCache.SetCap(gHOOKS_CACHE_LRU_SIZE) diff --git a/g/os/glog/glog.go b/g/os/glog/glog.go index 5f7416074..c6f2cf8c7 100644 --- a/g/os/glog/glog.go +++ b/g/os/glog/glog.go @@ -39,7 +39,7 @@ func New() *Logger { io : nil, path : gtype.NewString(), debug : gtype.NewBool(true), - btSkip : gtype.NewInt(3), + btSkip : gtype.NewInt(), stdprint : gtype.NewBool(true), } } @@ -64,6 +64,24 @@ func Cat(category string) *Logger { return logger.Cat(category) } +// 打印文件调用回溯信息 +func PrintBacktrace(skip...int) { + customSkip := 1 + if len(skip) > 0 { + customSkip += skip[0] + } + logger.PrintBacktrace(customSkip) +} + +// 获取文件调用回溯信息 +func GetBacktrace(skip...int) string { + customSkip := 1 + if len(skip) > 0 { + customSkip += skip[0] + } + return logger.GetBacktrace(customSkip) +} + // 设置写日志的同时开启or关闭控制台打印,默认是关闭的 // @author zseeker // @date 2018-05-24 diff --git a/g/os/glog/glog_logger.go b/g/os/glog/glog_logger.go index fce0a3b89..fe0629e0b 100644 --- a/g/os/glog/glog_logger.go +++ b/g/os/glog/glog_logger.go @@ -167,7 +167,7 @@ func (l *Logger) stdPrint(s string) { // 核心打印数据方法(标准错误) func (l *Logger) errPrint(s string) { // 记录调用回溯信息 - backtrace := l.backtrace() + backtrace := l.GetBacktrace(3) if s[len(s) - 1] == byte('\n') { s = s + backtrace + ln } else { @@ -176,15 +176,28 @@ func (l *Logger) errPrint(s string) { l.print(os.Stderr, s) } -// 调用回溯字符串 -func (l *Logger) backtrace() string { - backtrace := "Trace:" + ln +// 直接打印回溯信息,参数skip表示调用端往上多少级开始回溯 +func (l *Logger) PrintBacktrace(skip...int) { + customSkip := 1 + if len(skip) > 0 { + customSkip += skip[0] + } + l.Println(l.GetBacktrace(customSkip)) +} + +// 获取文件调用回溯字符串,参数skip表示调用端往上多少级开始回溯 +func (l *Logger) GetBacktrace(skip...int) string { + customSkip := 0 + if len(skip) > 0 { + customSkip += skip[0] + } + backtrace := "" index := 1 for i := 1; i < 10000; i++ { - if _, cfile, cline, ok := runtime.Caller(i + l.btSkip.Val()); ok { + if _, cfile, cline, ok := runtime.Caller(customSkip + i + l.btSkip.Val()); ok { // 不打印出go源码路径 if !gregex.IsMatchString("^" + gfile.GoRootOfBuild(), cfile) { - backtrace += strconv.Itoa(index) + ". " + cfile + ":" + strconv.Itoa(cline) + ln + backtrace += strconv.Itoa(index) + ".\t" + cfile + ":" + strconv.Itoa(cline) + ln index++ } } else { diff --git a/geg/database/orm/mysql/gdb_debug.go b/geg/database/orm/mysql/gdb_debug.go index 75a8af5c3..e7f8a364e 100644 --- a/geg/database/orm/mysql/gdb_debug.go +++ b/geg/database/orm/mysql/gdb_debug.go @@ -10,7 +10,7 @@ func main() { Host: "127.0.0.1", Port: "3306", User: "root", - Pass: "123456", + Pass: "8692651", Name: "test", Type: "mysql", Role: "master", @@ -31,5 +31,5 @@ func main() { db.Table("user").Data(g.Map{"name":"smith"}).Where("uid=?", 1).Save() - db.PrintQueriedSqls() + //db.PrintQueriedSqls() } \ No newline at end of file diff --git a/geg/os/glog/glog_backtrace.go b/geg/os/glog/glog_backtrace.go new file mode 100644 index 000000000..355de8e86 --- /dev/null +++ b/geg/os/glog/glog_backtrace.go @@ -0,0 +1,17 @@ +package main + +import ( + "gitee.com/johng/gf/g/os/glog" + "fmt" +) + +func main() { + + glog.PrintBacktrace() + glog.New().PrintBacktrace() + + fmt.Println(glog.GetBacktrace()) + fmt.Println(glog.New().GetBacktrace()) +} + +