diff --git a/logger.go b/logger.go index 575f2a0..5939318 100644 --- a/logger.go +++ b/logger.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "os" + "strings" "time" "github.com/pieterclaerhout/go-formatter" @@ -203,11 +204,31 @@ func FormattedStackTrace(err error) string { err = cause } - return eris.ToCustomString(eris.Wrap(err, err.Error()), eris.NewDefaultStringFormat(eris.FormatOptions{ + result := eris.ToCustomString(eris.Wrap(err, err.Error()), eris.NewDefaultStringFormat(eris.FormatOptions{ WithTrace: true, WithExternal: false, + InvertTrace: true, })) + resultLines := strings.Split(result, "\n") + + result = "" + for idx, line := range resultLines { + if strings.Contains(line, "go-log.") { + continue + } + if idx == 0 { + result += fmt.Sprintf("%s\n", line) + continue + } + lineParts := strings.SplitN(line, ":", 2) + if len(lineParts) == 2 { + result += fmt.Sprintf("%-50s %s\n", lineParts[0], lineParts[1]) + } + } + + return strings.TrimSuffix(result, "\n") + } // Fatal logs a fatal error message to stdout and exits the program with exit code 1 diff --git a/logger_test.go b/logger_test.go index 6f4ef97..e5e092f 100644 --- a/logger_test.go +++ b/logger_test.go @@ -525,6 +525,7 @@ func TestStackTrace(t *testing.T) { assert.Equal(t, "", actualStdOut) assert.True(t, strings.HasPrefix(actualStdErr, "test | ERROR | my error\n")) + assert.Equal(t, "test | ERROR | my error\n\tgo-log_test.TestStackTrace /Users/pclaerhout/Downloads/JonoFotografie/go-log/logger_test.go:521\n", actualStdErr) } @@ -548,13 +549,13 @@ func Test_StackTraceCustom(t *testing.T) { actualStdErr := stderr.String() assert.Equal(t, "", actualStdOut, "stdout") - assert.True(t, strings.HasPrefix(actualStdErr, "test | ERROR | boom\n"), "stderr") + assert.Equal(t, "test | ERROR | boom\n\tgo-log_test.Test_StackTraceCustom /Users/pclaerhout/Downloads/JonoFotografie/go-log/logger_test.go:546\n", actualStdErr) } func TestFormattedStackTrace(t *testing.T) { actual := log.FormattedStackTrace(errors.New("my error")) - assert.True(t, strings.HasPrefix(actual, "my error\n")) + assert.Equal(t, "my error\n\tgo-log_test.TestFormattedStackTrace /Users/pclaerhout/Downloads/JonoFotografie/go-log/logger_test.go:557", actual) } func TestFatal(t *testing.T) { @@ -581,7 +582,7 @@ func TestFatal(t *testing.T) { actualStdErr := stderr.String() assert.Equal(t, "", actualStdOut) - assert.True(t, strings.HasPrefix(actualStdErr, "test | FATAL | fatal error\n")) + assert.Equal(t, "test | FATAL | fatal error\n", actualStdErr) assert.Equal(t, 1, got) } @@ -610,7 +611,7 @@ func TestFatalf(t *testing.T) { actualStdErr := stderr.String() assert.Equal(t, "", actualStdOut) - assert.True(t, strings.HasPrefix(actualStdErr, "test | FATAL | fatal error 2\n")) + assert.Equal(t, "test | FATAL | fatal error 2\n", actualStdErr) assert.Equal(t, 1, got) }