打印堆栈信息
- 必须设置
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
- 返回error时必须使用这个包
github.com/pkg/errors
- 采用Err方法
zerolog.Error().Err(err)
- 使用
zerolog.Trace().Stack()
package main
import (
"bytes"
"fmt"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/pkgerrors"
)
func main() {
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
out := &bytes.Buffer{}
log := zerolog.New(out)
err := errors.Wrap(errors.New("error message"), "from error")
log.Trace().Stack().Err(err).Msg("")
//log.Trace().Stack().Err(err).Send()
fmt.Println(out.String())
// {"level":"trace","stack":[{"func":"main","line":"17","source":"main.go"},{"func"
//:"main","line":"203","source":"proc.go"},{"func":"goexit","line":"1357","source"
//:"asm_amd64.s"}],"error":"from error: error message"}
}