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语言操作数据库及其常规操作

Golang连接elasticsearch

Go-struct

Go语言中container容器数据结构heap、list、ring

手撸Golang 行为型设计模式 中介者模式

Golang 能开发前端吗?

聊聊dubbo-Go-proxy的hostfilter

Golang并发原语之-信号量semaphore

Golang gf怎么使用

2020.3.30日结

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




打赏

取消

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

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

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

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

评论

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