2019-01-02 19:12:56 +00:00
|
|
|
// +build !binary_log
|
|
|
|
|
2018-02-08 05:55:53 +00:00
|
|
|
package pkgerrors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"regexp"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLogStack(t *testing.T) {
|
|
|
|
zerolog.ErrorStackMarshaler = MarshalStack
|
|
|
|
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := zerolog.New(out)
|
|
|
|
|
|
|
|
err := errors.Wrap(errors.New("error message"), "from error")
|
|
|
|
log.Log().Stack().Err(err).Msg("")
|
|
|
|
|
|
|
|
got := out.String()
|
2019-01-02 19:12:56 +00:00
|
|
|
want := `\{"stack":\[\{"func":"TestLogStack","line":"20","source":"stacktrace_test.go"\},.*\],"error":"from error: error message"\}\n`
|
2018-02-08 05:55:53 +00:00
|
|
|
if ok, _ := regexp.MatchString(want, got); !ok {
|
|
|
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
2019-01-02 21:19:14 +00:00
|
|
|
|
|
|
|
func BenchmarkLogStack(b *testing.B) {
|
|
|
|
zerolog.ErrorStackMarshaler = MarshalStack
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
log := zerolog.New(out)
|
|
|
|
err := errors.Wrap(errors.New("error message"), "from error")
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
log.Log().Stack().Err(err).Msg("")
|
|
|
|
out.Reset()
|
|
|
|
}
|
|
|
|
}
|