diff --git a/errors/gerror/gerror.go b/errors/gerror/gerror.go index 6bb4614cc..8155331a5 100644 --- a/errors/gerror/gerror.go +++ b/errors/gerror/gerror.go @@ -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 - } -} diff --git a/errors/gerror/gerror_error_stack.go b/errors/gerror/gerror_error_stack.go index 598d8cacb..741daaa29 100644 --- a/errors/gerror/gerror_error_stack.go +++ b/errors/gerror/gerror_error_stack.go @@ -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 diff --git a/internal/errors/errors.go b/internal/errors/errors.go new file mode 100644 index 000000000..8ab919c12 --- /dev/null +++ b/internal/errors/errors.go @@ -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 +} diff --git a/internal/errors/errors_test.go b/internal/errors/errors_test.go new file mode 100644 index 000000000..e0ebab6eb --- /dev/null +++ b/internal/errors/errors_test.go @@ -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) + }) +} diff --git a/os/glog/glog_logger.go b/os/glog/glog_logger.go index 8455fa9d1..ff06fe7aa 100644 --- a/os/glog/glog_logger.go +++ b/os/glog/glog_logger.go @@ -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) }