From 3e7416ca7d76e9bd1ef22c113f96d6b1da0ebc38 Mon Sep 17 00:00:00 2001 From: John Date: Thu, 23 May 2019 19:14:16 +0800 Subject: [PATCH] add Skip for glog --- g/os/glog/glog_chaining.go | 7 +++++ g/os/glog/glog_logger.go | 50 ++++++++++++++++++++----------- g/os/glog/glog_logger_chaining.go | 14 +++++++++ geg/os/glog/glog_line2.go | 14 +++++++++ 4 files changed, 68 insertions(+), 17 deletions(-) create mode 100644 geg/os/glog/glog_line2.go diff --git a/g/os/glog/glog_chaining.go b/g/os/glog/glog_chaining.go index 231af31fa..6ac8c858b 100644 --- a/g/os/glog/glog_chaining.go +++ b/g/os/glog/glog_chaining.go @@ -40,6 +40,13 @@ func Level(level int) *Logger { return logger.Level(level) } +// Skip is a chaining function, +// which sets backtrace skip for the current logging content output. +// It also affects the caller file path checks when line number printing enabled. +func Skip(skip int) *Logger { + return logger.Skip(skip) +} + // Backtrace is a chaining function, // which sets backtrace options for the current logging content output . func Backtrace(enabled bool, skip...int) *Logger { diff --git a/g/os/glog/glog_logger.go b/g/os/glog/glog_logger.go index c7ce97c37..7935bca1e 100644 --- a/g/os/glog/glog_logger.go +++ b/g/os/glog/glog_logger.go @@ -307,22 +307,21 @@ func (l *Logger) GetBacktrace(skip...int) string { customSkip = skip[0] } backtrace := "" - index := 1 from := 0 - // 首先定位业务文件开始位置 - for i := 0; i < 10; i++ { + // Find the caller position exclusive of the glog file. + for i := 0; i < 100; i++ { if _, file, _, ok := runtime.Caller(i); ok { if !gregex.IsMatchString("/g/os/glog/glog.+$", file) { - from = i + from = i + 1 break } } } - // 从业务文件开始位置根据自定义的skip开始backtrace + // Find the true caller file path using custom skip. + index := 1 goRoot := runtime.GOROOT() for i := from + customSkip + l.btSkip; i < 10000; i++ { if _, file, cline, ok := runtime.Caller(i); ok && file != "" { - // 不打印出go源码路径及glog包文件路径,日志打印必须从业务源码文件开始,且从glog包文件开始检索 if (goRoot == "" || !gregex.IsMatchString("^" + goRoot, file)) && !gregex.IsMatchString(``, file) { backtrace += fmt.Sprintf(`%d. %s:%d%s`, index, file, cline, ln) index++ @@ -334,6 +333,34 @@ func (l *Logger) GetBacktrace(skip...int) string { return backtrace } +// getLongFile returns the absolute file path along with its line number of the caller. +func (l *Logger) getLongFile() string { + from := 0 + // Find the caller position exclusive of the glog file. + for i := 0; i < 100; i++ { + if _, file, line, ok := runtime.Caller(i); ok { + if !gregex.IsMatchString("/g/os/glog/glog.+$", file) { + from = i + 1 + break + return fmt.Sprintf(`%s:%d`, file, line) + } + } + } + // Find the true caller file path using custom skip. + goRoot := runtime.GOROOT() + for i := from + l.btSkip; i < 10000; i++ { + if _, file, line, ok := runtime.Caller(i); ok && file != "" { + if (goRoot == "" || !gregex.IsMatchString("^" + goRoot, file)) && !gregex.IsMatchString(``, file) { + return fmt.Sprintf(`%s:%d`, file, line) + } + } else { + break + } + } + return "" +} + + // format formats the content according the flags. func (l *Logger) format(content string) string { buffer := bytes.NewBuffer(nil) @@ -371,14 +398,3 @@ func (l *Logger) format(content string) string { return buffer.String() } -// getLongFile returns the absolute file path along with its line number of the caller. -func (l *Logger) getLongFile() string { - for i := 0; i < 100; i++ { - if _, file, line, ok := runtime.Caller(i); ok { - if !gregex.IsMatchString("/g/os/glog/glog.+$", file) { - return fmt.Sprintf(`%s:%d`, file, line) - } - } - } - return "" -} diff --git a/g/os/glog/glog_logger_chaining.go b/g/os/glog/glog_logger_chaining.go index 63e13a46b..ea6fb788f 100644 --- a/g/os/glog/glog_logger_chaining.go +++ b/g/os/glog/glog_logger_chaining.go @@ -81,6 +81,20 @@ func (l *Logger) Level(level int) *Logger { return logger } +// Skip is a chaining function, +// which sets backtrace skip for the current logging content output. +// It also affects the caller file path checks when line number printing enabled. +func (l *Logger) Skip(skip int) *Logger { + logger := (*Logger)(nil) + if l.parent == nil { + logger = l.Clone() + } else { + logger = l + } + logger.SetBacktraceSkip(skip) + return logger +} + // Backtrace is a chaining function, // which sets backtrace options for the current logging content output . func (l *Logger) Backtrace(enabled bool, skip...int) *Logger { diff --git a/geg/os/glog/glog_line2.go b/geg/os/glog/glog_line2.go new file mode 100644 index 000000000..454aedf57 --- /dev/null +++ b/geg/os/glog/glog_line2.go @@ -0,0 +1,14 @@ +package main + +import ( + "github.com/gogf/gf/g/os/glog" +) + +func print() { + glog.Line(true).Println("123") +} + +func main() { + glog.Line().Println("123") + glog.Line(true).Println("123") +}