使用github.com/pkg/errors打印详细的堆栈信息,输出非常友好。
设置堆栈打印详细调用过程
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
输出详细的堆栈信息
package main
import (
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/rs/zerolog/pkgerrors"
"github.com/thinkeridea/go-extend/exbytes"
"github.com/thinkeridea/go-extend/exstrings"
"os"
"strconv"
)
func C() error {
_, err := os.Open("err")
if err != nil {
return errors.WithStack(err)
}
return nil
}
func B() error {
return C()
}
func A() error {
return B()
}
func init() {
log.Logger = zerolog.New(os.Stderr).
With().Timestamp().Stack().CallerWithSkipFrameCount(2).Logger()
zerolog.CallerMarshalFunc = func(file string, line int) string {
return WashPath(file) +":"+ strconv.Itoa(line)
}
// 使用官方提供的,输出更友好
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
}
func main() {
err := A()
if err != nil {
log.Error().Stack().Err(err).Msg("")
}
}
// 路径脱敏
func WashPath(s string) string {
sb := exstrings.Bytes(s)
path, _ := os.Getwd()
pathByte := exbytes.Replace([]byte(path+"/"), []byte("\\"), []byte("/"), -1)
root := os.Getenv("GOROOT")
rootByte := exbytes.Replace([]byte(root+"/"), []byte("\\"), []byte("/"), -1)
sb = exbytes.Replace(sb, pathByte, []byte(""), -1)
sb = exbytes.Replace(sb, rootByte, []byte(""), -1)
return exbytes.ToString(sb)
}
效果
{
"level":"error",
"stack":[
{
"func":"C",
"line":"17",
"source":"main.go"
},
{
"func":"B",
"line":"23",
"source":"main.go"
},
{
"func":"A",
"line":"26",
"source":"main.go"
},
{
"func":"main",
"line":"51",
"source":"main.go"
},
{
"func":"main",
"line":"203",
"source":"proc.go"
},
{
"func":"goexit",
"line":"1373",
"source":"asm_amd64.s"
}
],
"error":"open err: The system cannot find the file specified.",
"time":"2020-03-27T20:15:03+08:00",
"caller":"zerolog_errors/main.go:55"
}
网友评论