improve error stack configuration for package gerror, add framework error stack filter for package glog (#2918)

This commit is contained in:
John Guo
2023-08-31 15:31:55 +08:00
committed by GitHub
parent 7d4c59ac5a
commit 3f69e0db36
5 changed files with 94 additions and 22 deletions

View File

@ -13,7 +13,6 @@ package gerror
import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/internal/command"
)
// IIs is the interface for Is feature.
@ -59,21 +58,6 @@ type IUnwrap interface {
}
const (
// commandEnvKeyForBrief is the command environment name for switch key for brief error stack.
commandEnvKeyForBrief = "gf.gerror.brief"
// commaSeparatorSpace is the comma separator with space.
commaSeparatorSpace = ", "
)
var (
// isUsingBriefStack is the switch key for brief error stack.
isUsingBriefStack bool
)
func init() {
value := command.GetOptWithEnv(commandEnvKeyForBrief)
if value == "1" || value == "true" {
isUsingBriefStack = true
}
}

View File

@ -14,6 +14,7 @@ import (
"strings"
"github.com/gogf/gf/v2/internal/consts"
"github.com/gogf/gf/v2/internal/errors"
)
// stackInfo manages stack info of certain error.
@ -35,9 +36,10 @@ func (err *Error) Stack() string {
return ""
}
var (
loop = err
index = 1
infos []*stackInfo
loop = err
index = 1
infos []*stackInfo
isStackModeBrief = errors.IsStackModeBrief()
)
for loop != nil {
info := &stackInfo{
@ -46,7 +48,7 @@ func (err *Error) Stack() string {
}
index++
infos = append(infos, info)
loopLinesOfStackInfo(loop.stack, info)
loopLinesOfStackInfo(loop.stack, info, isStackModeBrief)
if loop.error != nil {
if e, ok := loop.error.(*Error); ok {
loop = e
@ -131,14 +133,14 @@ func formatStackLines(buffer *bytes.Buffer, lines *list.List) string {
}
// loopLinesOfStackInfo iterates the stack info lines and produces the stack line info.
func loopLinesOfStackInfo(st stack, info *stackInfo) {
func loopLinesOfStackInfo(st stack, info *stackInfo, isStackModeBrief bool) {
if st == nil {
return
}
for _, p := range st {
if fn := runtime.FuncForPC(p - 1); fn != nil {
file, line := fn.FileLine(p - 1)
if isUsingBriefStack {
if isStackModeBrief {
// filter whole GoFrame packages stack paths.
if strings.Contains(file, consts.StackFilterKeyForGoFrame) {
continue