golang在日志中打印堆栈信息


本文摘自网络,作者,侵删。

使用debug.Stack()

package main
 
import ("fmt""runtime/debug")
 
func test1() {
    test2()
}
 
func test2() {
    test3()
}
 
func test3() {// 可以通过 debug.PrintStack() 直接打印,也可以通过 debug.Stack() 方法获取堆栈然后自己打印fmt.Printf("%s", debug.Stack())
    debug.PrintStack()
}
 
func main() {
    test1()
} 
------------------------------------
 $ go run test_stacktrace.go
 
goroutine 1 [running]:
runtime/debug.Stack(0x0, 0x0, 0x0)/usr/lib/golang/src/runtime/debug/stack.go:24 +0x80main.test3()/tmp/test_stacktrace.go:17 +0x24main.test2()/tmp/test_stacktrace.go:13 +0x14main.test1()/tmp/test_stacktrace.go:9 +0x14main.main()/tmp/test_stacktrace.go:22 +0x14
 goroutine 1 [running]:
runtime/debug.Stack(0x0, 0x0, 0x0)/usr/lib/golang/src/runtime/debug/stack.go:24 +0x80runtime/debug.PrintStack()/usr/lib/golang/src/runtime/debug/stack.go:16 +0x18main.test3()/tmp/test_stacktrace.go:18 +0x101main.test2()/tmp/test_stacktrace.go:13 +0x14main.test1()/tmp/test_stacktrace.go:9 +0x14main.main()/tmp/test_stacktrace.go:22 +0x14

 


本文来自:51CTO博客

感谢作者:mb601cf691d1fe2

查看原文:golang在日志中打印堆栈信息

相关阅读 >>

Go语言函数

云边端架构easydss内Go语言如何生成归档文件?

"b = &boy{}" vs "*b = boy{}" 谁不讲武德?Golang 逃逸分析入门

分享一些为phper准备的Go入门知识

Golang in pingcap

ubuntu下安装Golang

Golang slice检查元素是否存在

Golang如何设置字体

Golang json怎么隐藏某个字段

使用viper读取nacos配置(开源)

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...