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在日志中打印堆栈信息

相关阅读 >>

Golang并发模式之扇入fan-in和扇出fan-out

Go 大数据生态迎来重要产品 cds

Golang开发中包的使用

json序列化在Golang中的应用

Golang中...是什么意思?

手撸Golang 结构型设计模式 桥接模式

Golang实战群:日志的处理机制

一周 Go world 新鲜事

手撸Golang 架构设计原则 接口隔离原则

关于在c++中结构体与json字符串互转的问题

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




打赏

取消

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

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

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

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

评论

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