Golang: Find filenames and line numbers that prints output to stdout

这一生的挚爱 提交于 2020-01-07 06:28:30

问题


I inherited a codebase with tens of thousands of lines, and several libraries, that prints out several lines of garbage (both from normal functions and imported libraries, as well as from test functions and libraries) onto the console when I run a go test. This makes testing a nightmare since my print statements and errors get lost in a sea of junk.

Is there an easy way to identify and output the filenames and line numbers where any sort of printing to the console occurs, so that I can mute these suckers?


回答1:


Maybe adding some instrumentation to your legacy code could help.

For instance, spacemonkeygo/monkit can add trace graphs for you to at least identify from where those logs are generated.




回答2:


If the messages are printed using the log package, you could tell it to always include the full file path and line number where the calls happen:

log.SetFlags(log.LstdFlags|log.Llongfile)


来源:https://stackoverflow.com/questions/38536734/golang-find-filenames-and-line-numbers-that-prints-output-to-stdout

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!