From a86f4f8e23df0c8e558f13d8eafe1deb727f0a5e Mon Sep 17 00:00:00 2001 From: John Date: Tue, 18 Dec 2018 20:03:23 +0800 Subject: [PATCH] disable auto adding temp directory to gview/gcfg search path; disable backtrace feature in normal log print with glog; fix issue caused by fmt.Fprintf in gfsnotify --- g/frame/gins/gins.go | 6 ++++-- g/net/ghttp/ghttp_server.go | 2 +- g/os/gfsnotify/gfsnotify_watcher_loop.go | 2 +- g/os/glog/glog.go | 8 ++++++++ g/os/glog/glog_logger.go | 19 ++++++++++--------- g/os/gview/gview.go | 6 ++++-- g/util/gtest/gtest.go | 6 ++++-- geg/other/test.go | 16 ++-------------- 8 files changed, 34 insertions(+), 31 deletions(-) diff --git a/g/frame/gins/gins.go b/g/frame/gins/gins.go index 66598e940..9b65d0a8e 100644 --- a/g/frame/gins/gins.go +++ b/g/frame/gins/gins.go @@ -76,7 +76,8 @@ func View(name...string) *gview.View { if path == "" { path = genv.Get("GF_VIEWPATH") if path == "" { - if gfile.SelfDir() != gfile.TempDir() { + // gfile.MainPkgPath() 用以判断是否开发环境 + if gfile.MainPkgPath() == "" { path = gfile.SelfDir() } } @@ -105,7 +106,8 @@ func Config(file...string) *gcfg.Config { if path == "" { path = genv.Get("GF_CFGPATH") if path == "" { - if gfile.SelfDir() != gfile.TempDir() { + // gfile.MainPkgPath() 用以判断是否开发环境 + if gfile.MainPkgPath() == "" { path = gfile.SelfDir() } } diff --git a/g/net/ghttp/ghttp_server.go b/g/net/ghttp/ghttp_server.go index 40f340382..f31cc5a7e 100644 --- a/g/net/ghttp/ghttp_server.go +++ b/g/net/ghttp/ghttp_server.go @@ -257,7 +257,7 @@ func (s *Server) Start() error { } // 是否处于开发环境 if gfile.MainPkgPath() != "" { - glog.Backtrace(false, 0).Notice("GF notices that you're in develop environment, so error logs are auto enabled to stdout.") + glog.Debug("GF notices that you're in develop environment, so error logs are auto enabled to stdout.") } // 打印展示路由表 diff --git a/g/os/gfsnotify/gfsnotify_watcher_loop.go b/g/os/gfsnotify/gfsnotify_watcher_loop.go index 9f45d5dc8..34401e307 100644 --- a/g/os/gfsnotify/gfsnotify_watcher_loop.go +++ b/g/os/gfsnotify/gfsnotify_watcher_loop.go @@ -34,7 +34,7 @@ func (w *Watcher) startWatchLoop() { }, REPEAT_EVENT_FILTER_INTERVAL) case err := <- w.watcher.Errors: - fmt.Fprintf(os.Stderr, "error: %s\n" + err.Error()) + fmt.Fprintf(os.Stderr, "[gfsnotify] error: %s\n", err.Error()) } } }() diff --git a/g/os/glog/glog.go b/g/os/glog/glog.go index b213987db..6820b2383 100644 --- a/g/os/glog/glog.go +++ b/g/os/glog/glog.go @@ -11,6 +11,7 @@ package glog import ( "gitee.com/johng/gf/g/container/gtype" + "gitee.com/johng/gf/g/os/gfile" "io" ) @@ -32,6 +33,13 @@ var ( logger = New() ) +func init() { + // 自动识别开发环境 + if gfile.MainPkgPath() != "" { + logger.SetDebug(true) + } +} + // 日志日志目录绝对路径 func SetPath(path string) { logger.SetPath(path) diff --git a/g/os/glog/glog_logger.go b/g/os/glog/glog_logger.go index 4b022cf48..cd147ffe7 100644 --- a/g/os/glog/glog_logger.go +++ b/g/os/glog/glog_logger.go @@ -219,9 +219,6 @@ func (l *Logger) doStdLockPrint(std io.Writer, s string) { // 核心打印数据方法(标准输出) func (l *Logger) stdPrint(s string) { - if l.btStatus.Val() == 1 { - s = l.appendBacktrace(s) - } l.print(os.Stdout, s) } @@ -237,14 +234,18 @@ func (l *Logger) errPrint(s string) { } // 输出内容中添加回溯信息 -func (l *Logger) appendBacktrace(s string) string { - trace := l.GetBacktrace() +func (l *Logger) appendBacktrace(s string, skip...int) string { + trace := l.GetBacktrace(skip...) if trace != "" { backtrace := "Backtrace:" + ln + trace - if s[len(s) - 1] == byte('\n') { - s = s + backtrace + ln + if len(s) > 0 { + if s[len(s)-1] == byte('\n') { + s = s + backtrace + ln + } else { + s = s + ln + backtrace + ln + } } else { - s = s + ln + backtrace + ln + s = backtrace } } return s @@ -252,7 +253,7 @@ func (l *Logger) appendBacktrace(s string) string { // 直接打印回溯信息,参数skip表示调用端往上多少级开始回溯 func (l *Logger) PrintBacktrace(skip...int) { - l.Println(l.GetBacktrace(skip...)) + l.Println(l.appendBacktrace("", skip...)) } // 获取文件调用回溯字符串,参数skip表示调用端往上多少级开始回溯 diff --git a/g/os/gview/gview.go b/g/os/gview/gview.go index 4eb9eb296..bff5b7ef8 100644 --- a/g/os/gview/gview.go +++ b/g/os/gview/gview.go @@ -49,10 +49,12 @@ var viewObj *View // 初始化默认的视图对象 func checkAndInitDefaultView() { if viewObj == nil { - if gfile.SelfDir() != gfile.TempDir() { + // gfile.MainPkgPath() 用以判断是否开发环境 + mainPkgPath := gfile.MainPkgPath() + if gfile.MainPkgPath() == "" { viewObj = New(gfile.SelfDir()) } else { - viewObj = New() + viewObj = New(mainPkgPath) } } } diff --git a/g/util/gtest/gtest.go b/g/util/gtest/gtest.go index 2b054afa1..f763cfede 100644 --- a/g/util/gtest/gtest.go +++ b/g/util/gtest/gtest.go @@ -18,13 +18,15 @@ import ( // 断言判断 func Assert(value, expect interface{}) { if gconv.String(value) != gconv.String(expect) { - glog.Backtrace(true, 1).Printfln(`[ASSERT] VALUE: %v, EXPECT: %v`, value, expect) + glog.Printfln(`[ASSERT] VALUE: %v, EXPECT: %v`, value, expect) + glog.Header(false).PrintBacktrace(1) os.Exit(1) } } // 提示错误并退出 func Fatal(message...interface{}) { - glog.Backtrace(true, 1).Println(`[FATAL] `, fmt.Sprint(message...)) + glog.Println(`[FATAL] `, fmt.Sprint(message...)) + glog.Header(false).PrintBacktrace(1) os.Exit(1) } \ No newline at end of file diff --git a/geg/other/test.go b/geg/other/test.go index 4f81b1117..9c4bfcb22 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -1,19 +1,7 @@ package main -import ( - "fmt" - "gitee.com/johng/gf/g" - "gitee.com/johng/gf/g/util/gregex" -) +import "gitee.com/johng/gf/g/util/gtest" func main() { - //s := `1544180795 -- s_has_sess -- 41570504 -decryptSess- 41570504__iuVycRYg9qE3y7CsSgGZH1K2nxTdjPZN4fXot65zHIEmULO0Ow6LweJp5raWl8Ft -postSess- eyJpdiI6IkFwSWZ3eXFMcGxBZE5JcWF4aXh0M3c9PSIsInZhbHVlIjoiV3ZLeGduMnRoRkFZdmxHTzM5ZzdyU1JHWDMycmZlRERvNnFkaUR0SitlRjBrZnlYR1JvS2puTGZNUThSeFR0bWtlT3pza0l0elFqRk5mdXF6XC9FWWpWZnljVjdJbHd3dTRybEhldHZHTk5DQ015dlpYNHljNmxKMWJTRUVpY0E4IiwibWFjIjoiOTkxMzIxOTRhMGUxZWZiODM4NWZjNDZjYmVhNWY2NjhlZDZkNmVlNjY1MTE2N2VhZDAzYzY4NDJmZGFkMjY5YyJ9 -- 0 -- B8105CF2-1588-4753-9F86-9B8C36EB1842 -- iPhone 7 -- 12.1 -- 6.8.7 -- i -- 10.111.153.5 -- medlinker -- service -- unknown -//` -// s := `[08-Dec-2018 13:35:03 Asia/Shanghai] Medlinker\Services\Message\MessageService|updateUserInfo|用户头像 URI 不能为空 in /var/www/med-d2d/app/Services/Message/RongCloudService.php on line 851` - s := `[2018-12-01 13:35:03 Asia/Shanghai] 1544180795 Medlinker\Services\Message\MessageService|updateUserInfo|用户头像 URI 不能为空 in /var/www/med-d2d/app/Services/Message/RongCloudService.php on line 851` - //m, e := gregex.MatchString(`/var/log/medlinker/[\w\-\_]+/(.+?)/{0,1}[\d\-\_]*\.log`, `/var/log/medlinker/med-questionnaire/nginx/error/access-20181206.log`) - //m, e := gregex.MatchString(`/var/log/medlinker/[\w\-\_]+/(.+?)/{0,1}[\d\-\_]*\.log`, `/var/log/medlinker/med-questionnaire/storagelogs/events/sqlLog/2018-12-06.log`) - m, e := gregex.MatchString(`(.*?((\d{4}[-/\.]\d{2}[-/\.]\d{2}|\d{1,2}[-/\.][A-Za-z]{3,}[-/\.]\d{4})[:\sT-]*\d{0,2}:{0,1}\d{0,2}:{0,1}\d{0,2}\.{0,1}\d{0,9}[\sZ]{0,1}[\+-]{0,1}[:\d]*|\d{10}).+)`, s) - fmt.Println(e) - g.Dump(m) + gtest.Assert(1, 2) }