(Golang)如何限制命令行输出的数量

huangapple go评论90阅读模式
英文:

(Golang) How to restrict ammount of command line output

问题

在执行某个 Golang 应用程序时,当发生 panic 时,命令行的显示被错误消息淹没了。假设有很多 goroutine 在运行,这种情况可能会发生。

然而,我只想知道输出的前几行信息。我该如何限制我想要的这个功能呢?

谢谢。

Harry

英文:

During executing something golang application, display of command line is overflowed by error messages
when something panic occured.
Provided there were lots of goroutine running, it could'be appeared.

However, information I want to know is just a few lines from top of output.
How can I restrict that feature I desire?

Thank you.

Harry

答案1

得分: 3

两种解决方案值得考虑。你可以选择适合你的方法。

  1. Panicparse

Marc-Antoine Ruel是一位出色的开发者,他创建了Panicparse。运行你的程序,将stderr赋值给stdout,将其导入panicparse,然后你就可以开始使用了。

go run example.go 2>&1 | pp

Panicparse将对输出进行去重和精简,使其可读性大大提高。

请在README中查看屏幕截图。

  1. Head

如果你要查找的信息总是在堆栈跟踪的顶部,那么可能只需要将输出导入head即可。

我们希望运行程序,让它像正常情况下输出到stdout/终端,但我们希望使用head来限制panic显示的信息量。请记住,panic是写入stderr的。因此,在大多数系统上,将stderr导入到进程替换中应该可以工作:

go run example.go 2> >(head)
英文:

Two solutions come to mind. You can pick what works for you.

  1. Panicparse

Marc-Antoine Ruel -- fantastic developer -- created panicparse. Run your program, assign stderr to stdout, pipe that into panicparse, and you're good to go.

go run example.go 2>&1 | pp

Panicparse will deduplicate and dedensify the output >50% while also making it far more readable.

Check out the screenshots in the README.

  1. Head

If the information you're looking for is always at the top of the stacktrace, then maybe it's just a simple case of piping the output to head.

We want to run our program; allowing it to output to stdout/terminal like normal, but we want to limit the amount of information the panic displays with head. Remember, panics are written to stderr. So piping stderr to a process substitution should work on most systems:

go run example.go 2> >(head)

huangapple
  • 本文由 发表于 2016年2月23日 12:43:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/35568993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定