GO 优雅追踪堆栈错误包

Golang tracks stack error package. 优雅追踪堆栈错误包

安装(Install)

go get github.com/yezihack/e

介绍(Introduction)

github.com/yezihack/e 项目是一个优雅地追踪你的堆栈信息.方便存储日志里.
而且还扩展了error包,自定义 code,msg 信息.

特色(Features)

优雅地追踪堆栈错误信息

  1. 基于github.com/pkg/errors包进行封装
  2. 支持 code, msg 自定义错误码和错误信息
  3. 方便存储日志json文件
  4. 堆栈信息以人性化展示

文档(Documentation)

https://godoc.org/github.com/yezihack/e

简单使用(Use)

package main
import (
    "github.com/yezihack/e"
    "log"
)
func foo() error {
    return e.New("foo")
}
func main() {
    err := foo()
    if err != nil { // 需要判断是否是自定义error, 否则无法输出堆栈信息.
        if e.Assert(err)  {
            log.Println(e.Convert(err).ToStr()) // 输出字符串形式
            log.Println(e.Convert(err).ToArr()) // 输出数组形式
        } else {
            log.Println(err) // 系统的 error
        }
    }
}

与原堆栈信息对比

github.com/pkg/errors 原生输出的.

仅使用 fmt.Printf("%+v", err) 输出

// Example output:
    // whoops
    // github.com/pkg/errors_test.ExampleNew_printf
    //         /home/dfc/src/github.com/pkg/errors/example_test.go:17
    // testing.runExample
    //         /home/dfc/go/src/testing/example.go:114
    // testing.RunExamples
    //         /home/dfc/go/src/testing/example.go:38
    // testing.(*M).Run
    //         /home/dfc/go/src/testing/testing.go:744
    // main.main
    //         /github.com/pkg/errors/_test/_testmain.go:106
    // runtime.main
    //         /home/dfc/go/src/runtime/proc.go:183
    // runtime.goexit
    //         /home/dfc/go/src/runtime/asm_amd64.s:2059

github.com/yezihack/e 输出的.

可以进行字符串输出, 也可以以切片输出

file:1.how_test.go, line:13, func:foo
file:1.how_test.go, line:18, func:TestFoo.func1
file:discovery.go, line:80, func:parseAction.func1
file:context.go, line:261, func:(*context).conveyInner
file:context.go, line:110, func:rootConvey.func1
file:context.go, line:97, func:(*ContextManager).SetValues.func1
file:gid.go, line:24, func:EnsureGoroutineId.func1
file:stack_tags.go, line:108, func:_m
file:stack_tags.go, line:56, func:github_com_jtolds_gls_markS
file:stack_tags.go, line:49, func:addStackTag
file:gid.go, line:24, func:EnsureGoroutineId
file:context.go, line:63, func:(*ContextManager).SetValues
file:context.go, line:105, func:rootConvey
file:doc.go, line:75, func:Convey
file:1.how_test.go, line:17, func:TestFoo

实例(Example)

  1. 基本用法
  2. Code用法
  3. 兼容老项目里的 error
  4. 获取 extra 的扩展错误
  5. gin中使用
  6. 更多等待更新中...

输出普通信息和堆栈信息(string or array)

package main

import (
    "log"

    "github.com/yezihack/e"
)

func foo(s string) error {
    return e.New("foo," + s)
}

func main() {
    // (1)普通使用
    err := foo("stack error")
    if err != nil {
        log.Println(err)
    }
    // out:
    // 2021/01/15 20:23:21 foo,stack error

    // (2)输出堆栈信息 by string
    if e.Assert(err) { // 需要判断是否是自定义error, 否则无法输出堆栈信息.
        log.Println(e.Convert(err).ToStr())
    }
    // out:
    //2021/01/15 20:23:21 file:1.how.go, line:10, func:foo
    //file:1.how.go, line:15, func:main

    // (3)输出堆栈信息 by array
    if e.Assert(err) { // 需要判断是否是自定义error, 否则无法输出堆栈信息.
        log.Println(e.Convert(err).ToArr())
    }
    // out
    //2021/01/15 20:23:21 [file:1.how.go, line:10, func:foo file:1.how.go, line:15, func:main]
}

带自定义code的错误信息

package main

import (
    "fmt"

    "github.com/yezihack/e"
)

// 如果使用带 code 的 error
func fooCode() error {
    return e.NewCode(400, "eoo error")
}

func main() {
    err := fooCode()
    if e.Assert(err) {
        e1 := e.Convert(err)
        fmt.Printf("code:%d, err:%s\n", e1.Code(), e1.Msg())
    }
    //out:
    //code:400, err:eoo error
    if e.Assert(err) {
        e1 := e.Convert(err)
        fmt.Printf("code:%d, err:%s\n", e1.Code(), e1.ToStr())
    }
    // out:
    //code:400, err:file:2.code.go, line:11, func:fooCode
    //file:2.code.go, line:15, func:main
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • - 后端早读课翻译计划 第四篇- - 翻译自: a-journey-with-go 欢迎关注微信公众号: 后端早读...
    cd50850d83d8阅读 512评论 0 0
  • 理解不透彻,实在是无法写清楚,记录下过程吧 为了理解堆栈区别,我对比 c++,java,APP,javascipt...
    小王同学加油阅读 1,126评论 0 0
  • 在讲Go的堆栈之前,先温习一下堆栈基础知识。 什么是堆栈?在计算机中堆栈的概念分为:数据结构的堆栈和内存分配中堆栈...
    guyan0319阅读 819评论 0 1
  • Go 语言实战: 编写可维护 Go 语言代码建议 目录 1. 指导原则1.1 简单性1.2 可读性1.3 生产力...
    DukeAnn阅读 1,012评论 0 5
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,168评论 19 139