Skip to content

Commit

Permalink
Improved the logging of stack traces
Browse files Browse the repository at this point in the history
  • Loading branch information
pieterclaerhout committed May 25, 2020
1 parent badf63b commit 057e2fd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
23 changes: 22 additions & 1 deletion logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"strings"
"time"

"github.com/pieterclaerhout/go-formatter"
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}

Expand All @@ -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) {
Expand All @@ -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)

}
Expand Down Expand Up @@ -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)

}
Expand Down

0 comments on commit 057e2fd

Please sign in to comment.