Skip to content

Commit

Permalink
improved error value and trace info
Browse files Browse the repository at this point in the history
  • Loading branch information
hedzr committed Nov 17, 2024
1 parent 9b58ba5 commit 2044821
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 16 deletions.
2 changes: 2 additions & 0 deletions bench/slog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func Test2(t *testing.T) {
}

func TestUseJSON(t *testing.T) {
defer slogg.SaveFlagsAndMod(slogg.Lcaller)()

logger := slogg.New().SetWriter(os.Stdout).SetLevel(slogg.DebugLevel).SetJSONMode()
msg, attrs := getMessage(0), fakeLoggArgs()
logger.Info(msg, attrs...)
Expand Down
5 changes: 4 additions & 1 deletion slog/attr.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ func (s Attrs) SerializeValueTo(pc *PrintCtx) {
_ = serializeAttrs(pc, s)
}

// serializeAttrs returns an error object if it's found in the given Attrs.
// The caller can do something with the object, For instance, printImpl
// will dump the error's stack trace if necessary.
func serializeAttrs(pc *PrintCtx, kvps Attrs) (err error) { //nolint:revive
prefix := pc.prefix
for _, v := range kvps {
Expand Down Expand Up @@ -162,7 +165,7 @@ func serializeAttrs(pc *PrintCtx, kvps Attrs) (err error) { //nolint:revive
} else {
pc.appendValue(val)
if e, ok := val.(error); ok && e != nil {
err = e
err = e // just a error value
}
}
pc.prefix = prefix
Expand Down
1 change: 1 addition & 0 deletions slog/cmn.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ const (
clrAttrKeyBg = clrNone
clrLoggerName = color.FgLightGray
clrLoggerNameBg = clrNone
clrError = color.FgRed
)

const (
Expand Down
55 changes: 41 additions & 14 deletions slog/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/hedzr/is"
"github.com/hedzr/is/stringtool"
errorsv3 "gopkg.in/hedzr/errors.v3"
)

func newentry(parent *Entry, args ...any) *Entry {
Expand Down Expand Up @@ -1215,26 +1216,52 @@ func (s *Entry) printImpl(ctx context.Context, pc *PrintCtx) {
s.printRestLinesOfMsg(ctx, pc)

if holded != nil && (is.DebugMode() || is.DebugBuild()) {
var stackInfo string
if _, ok := holded.(interface{ Format(s fmt.State, verb rune) }); ok {
stackInfo = fmt.Sprintf("%+v", holded)
} else {
var x Stringer
if x, ok = holded.(Stringer); ok {
stackInfo = x.String() // special for those illegal error impl like toml.DecodeError, which have no Format
} else {
stackInfo = fmt.Sprintf("%v", holded)
}
}
if pc.jsonMode {
// pc.pcAppendStringKey("errDetail")
// pc.pcAppendColon()
// pc.pcAppendStringValue(stackInfo)
// pc.pcAppendComma()
} else {
pc.pcAppendByte('\n')
txt := ct.pad(stackInfo, " ", 1)
ct.wrapColorAndBgTo(pc, red, clrLoggerNameBg, txt)
testing := is.InTesting()
if testing {
var e3 errorsv3.Error
if errorsv3.As(holded, &e3) {
if f, ok := e3.(*errorsv3.WithStackInfo); ok {
if st := f.StackTrace(); st != nil {
pc.pcAppendByte('\n')

frame := st[0]
src := getpcsource(uintptr(frame))
pc.pcAppendStringKey(" error: ")
ct.wrapColorAndBgTo(pc, clrError, clrNone, holded.Error())
pc.pcAppendByte('\n')
pc.pcAppendStringKey(" file/line: ")
pc.pcAppendString(src.File)
pc.pcAppendRune(':')
pc.appendValue(src.Line)
pc.pcAppendByte('\n')
pc.pcAppendStringKey(" function: ")
ct.wrapColorAndBgTo(pc, clrFuncName, clrNone, src.Function)
pc.pcAppendByte('\n')
}
}
}
} else {
var stackInfo string
if _, ok := holded.(interface{ Format(s fmt.State, verb rune) }); ok {
stackInfo = fmt.Sprintf("%+v", holded)
} else {
var x Stringer
if x, ok = holded.(Stringer); ok {
stackInfo = x.String() // special for those illegal error impl like toml.DecodeError, which have no Format
} else {
stackInfo = fmt.Sprintf("%v", holded)
}
}
pc.pcAppendByte('\n')
txt := ct.pad(stackInfo, " ", 1)
ct.wrapColorAndBgTo(pc, red, clrLoggerNameBg, txt)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion slog/pc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ func (s *PrintCtx) appendError(err error) {
var e3 errorsv3.Error
if errors.As(err, &e3) {
if f, ok := e3.(*errorsv3.WithStackInfo); ok {
if st := f.Stack.StackTrace(); st != nil {
if st := f.StackTrace(); st != nil {
s.pcAppendComma()
s.pcAppendStringKey("trace")
s.pcAppendColon()
Expand Down

0 comments on commit 2044821

Please sign in to comment.