1
0
mirror of https://github.com/gogf/gf.git synced 2025-04-05 03:05:05 +08:00

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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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

61
internal/errors/errors.go Normal file
View File

@ -0,0 +1,61 @@
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package errors provides functionalities to manipulate errors for internal usage purpose.
package errors
import (
"github.com/gogf/gf/v2/internal/command"
)
// StackMode is the mode that printing stack information in StackModeBrief or StackModeDetail mode.
type StackMode string
const (
// commandEnvKeyForBrief is the command environment name for switch key for brief error stack.
// Deprecated: use commandEnvKeyForStackMode instead.
commandEnvKeyForBrief = "gf.gerror.brief"
// commandEnvKeyForStackMode is the command environment name for switch key for brief error stack.
commandEnvKeyForStackMode = "gf.gerror.stack.mode"
)
const (
// StackModeBrief specifies all error stacks printing no framework error stacks.
StackModeBrief StackMode = "brief"
// StackModeDetail specifies all error stacks printing detailed error stacks including framework stacks.
StackModeDetail StackMode = "detail"
)
var (
// stackModeConfigured is the configured error stack mode variable.
// It is brief stack mode in default.
stackModeConfigured = StackModeBrief
)
func init() {
// Deprecated.
briefSetting := command.GetOptWithEnv(commandEnvKeyForBrief)
if briefSetting == "1" || briefSetting == "true" {
stackModeConfigured = StackModeBrief
}
// The error stack mode is configured using command line arguments or environments.
stackModeSetting := command.GetOptWithEnv(commandEnvKeyForStackMode)
if stackModeSetting != "" {
stackModeSettingMode := StackMode(stackModeSetting)
switch stackModeSettingMode {
case StackModeBrief, StackModeDetail:
stackModeConfigured = stackModeSettingMode
}
}
}
// IsStackModeBrief returns whether current error stack mode is in brief mode.
func IsStackModeBrief() bool {
return stackModeConfigured == StackModeBrief
}

View File

@ -0,0 +1,20 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package errors_test
import (
"testing"
"github.com/gogf/gf/v2/internal/errors"
"github.com/gogf/gf/v2/test/gtest"
)
func Test_IsStackModeBrief(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
t.Assert(errors.IsStackModeBrief(), true)
})
}

View File

@ -21,6 +21,7 @@ import (
"github.com/gogf/gf/v2/debug/gdebug"
"github.com/gogf/gf/v2/internal/consts"
"github.com/gogf/gf/v2/internal/errors"
"github.com/gogf/gf/v2/internal/intlog"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/os/gfile"
@ -411,5 +412,9 @@ func (l *Logger) GetStack(skip ...int) string {
if l.config.StFilter != "" {
filters = append(filters, l.config.StFilter)
}
// Whether filter framework error stacks.
if errors.IsStackModeBrief() {
filters = append(filters, consts.StackFilterKeyForGoFrame)
}
return gdebug.StackWithFilters(filters, stackSkip)
}