From 9f17fd512022b156220ddb5ef34b3f0a9e7cea1c Mon Sep 17 00:00:00 2001 From: Pieter Claerhout Date: Tue, 24 Mar 2020 08:45:43 +0100 Subject: [PATCH] Improved the formatting and reliability of the stack traces --- .vscode/settings.json | 5 +++++ logger.go | 17 ++++++++++++++--- logger_internal.go | 22 ---------------------- logger_internal_test.go | 30 ------------------------------ logger_test.go | 29 ++++++++++++++++++++++++++--- 5 files changed, 45 insertions(+), 58 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..29182e8 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "go.testFlags": [ + "-v" + ] +} \ No newline at end of file diff --git a/logger.go b/logger.go index 356ecc1..224ede3 100644 --- a/logger.go +++ b/logger.go @@ -7,7 +7,7 @@ import ( "strings" "time" - "github.com/go-errors/errors" + "github.com/pkg/errors" "github.com/sanity-io/litter" "github.com/pieterclaerhout/go-formatter" @@ -199,10 +199,21 @@ func StackTrace(err error) { // FormattedStackTrace returns a formatted stacktrace for err func FormattedStackTrace(err error) string { - if cause := causeOfError(err); cause != nil { + + if cause := errors.Cause(err); cause != nil { err = cause } - return strings.TrimSpace(errors.Wrap(err, 2).ErrorStack()) + + type stackTracer interface { + StackTrace() errors.StackTrace + } + + if err, ok := err.(stackTracer); ok { + return strings.TrimSpace(fmt.Sprintf("%+v", err)) + } + + return strings.TrimSpace(fmt.Sprintf("%+v", err)) + } // Fatal logs a fatal error message to stdout and exits the program with exit code 1 diff --git a/logger_internal.go b/logger_internal.go index 0b7e96f..7a25fd8 100644 --- a/logger_internal.go +++ b/logger_internal.go @@ -4,14 +4,11 @@ import ( "fmt" "os" "strings" - "sync" "time" "github.com/fatih/color" ) -var logMutex = &sync.Mutex{} - var colors = map[string]*color.Color{ "DEBUG": color.New(color.FgHiBlack), "INFO ": color.New(color.FgHiGreen), @@ -65,7 +62,6 @@ func printMessage(level string, message string) { } if PrintColors { - // cw := colorable.NewColorable(w) if c, ok := colors[level]; ok { c.Fprintln(w, message) return @@ -75,21 +71,3 @@ func printMessage(level string, message string) { w.Write([]byte(message + "\n")) } - -func causeOfError(err error) error { - - type causer interface { - Cause() error - } - - for err != nil { - cause, ok := err.(causer) - if !ok { - break - } - err = cause.Cause() - } - - return err - -} diff --git a/logger_internal_test.go b/logger_internal_test.go index ce7274b..294aa35 100644 --- a/logger_internal_test.go +++ b/logger_internal_test.go @@ -6,7 +6,6 @@ import ( "testing" "time" - "github.com/pkg/errors" "github.com/stretchr/testify/assert" ) @@ -96,35 +95,6 @@ func TestPrintMessage(t *testing.T) { } -type customError struct { - message string -} - -var errCustomErrorCause = errors.New("cause of error") - -func newCustomError(message string) *customError { - return &customError{ - message: message, - } -} - -func (e *customError) Error() string { - return e.message -} - -func (e *customError) Cause() error { - return errCustomErrorCause -} - -func TestCauseOfError(t *testing.T) { - - err := newCustomError("custom error") - cause := causeOfError(err) - - assert.EqualValues(t, errCustomErrorCause, cause) - -} - func resetLogConfig() { PrintTimestamp = false DebugMode = false diff --git a/logger_test.go b/logger_test.go index 653af0a..ad7adeb 100644 --- a/logger_test.go +++ b/logger_test.go @@ -485,13 +485,36 @@ func TestStackTrace(t *testing.T) { actualStdErr := stderr.String() assert.Equal(t, "", actualStdOut) - assert.True(t, strings.HasPrefix(actualStdErr, "test | ERROR | *errors.fundamental my error\n")) + assert.True(t, strings.HasPrefix(actualStdErr, "test | ERROR | my error\n")) + +} + +type CustomError struct{} + +func (m *CustomError) Error() string { + return "boom" +} + +func Test_StackTraceCustom(t *testing.T) { + + resetLogConfig() + stdout, stderr := redirectOutput() + defer resetLogOutput() + + log.StackTrace(&CustomError{}) + + actualStdOut := stdout.String() + actualStdErr := stderr.String() + + assert.Equal(t, "", actualStdOut, "stdout") + t.Log(actualStdErr) + assert.True(t, strings.HasPrefix(actualStdErr, "test | ERROR | boom\n"), "stderr") } func TestFormattedStackTrace(t *testing.T) { actual := log.FormattedStackTrace(errors.New("my error")) - assert.True(t, strings.HasPrefix(actual, "*errors.fundamental my error\n")) + assert.True(t, strings.HasPrefix(actual, "my error\n")) } func TestFatal(t *testing.T) { @@ -564,7 +587,7 @@ func TestCheckError(t *testing.T) { {"nil-debug-color", nil, true, "", "", -1}, {"err-release-nocolor", errors.New("test"), false, "", "test | FATAL | test\n", 1}, - {"err-debug-nocolor", errors.New("test"), true, "", "test | FATAL | *errors.fundamental test\n", 1}, + {"err-debug-nocolor", errors.New("test"), true, "", "test | FATAL | test\n", 1}, } for _, tc := range tests {