add Skip for glog

This commit is contained in:
John
2019-05-23 19:14:16 +08:00
parent 162e8f7e51
commit 3e7416ca7d
4 changed files with 68 additions and 17 deletions

View File

@ -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 {

View File

@ -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(`<autogenerated>`, 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(`<autogenerated>`, 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 ""
}

View File

@ -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 {

14
geg/os/glog/glog_line2.go Normal file
View File

@ -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")
}