Golang: Find filenames and line numbers that print

2019-09-10 22:33发布

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?

标签: testing go
2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-09-10 23:14

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.

查看更多
祖国的老花朵
3楼-- · 2019-09-10 23:24

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)
查看更多
登录 后发表回答