Go優雅追蹤堆棧錯誤包


github.com/yezihack/e

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
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM