Skip to content

Commit

Permalink
feat(plc4go): better stack rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Aug 13, 2024
1 parent a25272c commit 3371f09
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
12 changes: 11 additions & 1 deletion plc4go/spi/testutils/TestUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,17 @@ func ProduceTestingLogger(t TestingLog) zerolog.Logger {
logger = logger.With().Timestamp().Logger()
}
stackSetter.Do(func() {
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
zerolog.ErrorStackMarshaler = func(err error) interface{} {
var r strings.Builder
stack := pkgerrors.MarshalStack(err).([]map[string]string)
for _, entry := range stack {
stackSourceFileName := entry[pkgerrors.StackSourceFileName]
stackSourceLineName := entry[pkgerrors.StackSourceLineName]
stackSourceFunctionName := entry[pkgerrors.StackSourceFunctionName]
r.WriteString(fmt.Sprintf("\tat %v (%v:%v)\n", stackSourceFunctionName, stackSourceFileName, stackSourceLineName))
}
return r.String()
}
})
logger = logger.With().Stack().Logger()
return logger
Expand Down
15 changes: 15 additions & 0 deletions plc4go/spi/testutils/TestUtils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package testutils
import (
"context"
"github.com/apache/plc4x/plc4go/spi/utils"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
"testing"
Expand Down Expand Up @@ -63,6 +64,20 @@ func (A *ASerializable) String() string {
return wbbb.GetBox().String()
}

func TestProduceTestingLogger_BetterStackrendering(t *testing.T) {
got := ProduceTestingLogger(t)
f1 := func() error {
return errors.New("a error")
}
f2 := func() error {
return errors.Wrap(f1(), "b error")
}
f3 := func() error {
return errors.Wrap(f2(), "c error")
}
got.Error().Err(f3()).Msg("multiline error")
}

func TestProduceTestingLogger_ASerializableLog(t *testing.T) {
got := ProduceTestingLogger(t)
aSerializable := &ASerializable{
Expand Down

0 comments on commit 3371f09

Please sign in to comment.