如何在Go Colly爬虫中屏蔽'Max depth limit reached'日志输出
屏蔽Colly爬虫的'Max depth limit reached'日志方法
Colly默认输出的Max depth limit reached属于Info级别日志,干扰自定义输出时可以通过以下几种方式解决:
调整日志级别,过滤Info级日志
直接将Collector的日志级别设置为ErrorLevel及以上,这样Info级别的提示就不会输出了:import ( "github.com/gocolly/colly/v2" ) func main() { c := colly.NewCollector( colly.LogLevel(colly.ErrorLevel), // 仅输出错误及更高级别的日志 ) // 你的爬虫逻辑... }Colly的日志级别从低到高依次为:
DebugLevel、InfoLevel、WarningLevel、ErrorLevel、FatalLevel,默认是InfoLevel。精准过滤单条日志
如果需要保留其他Info级日志,仅屏蔽特定的Max depth limit reached,可以自定义日志处理函数:import ( "log" "github.com/gocolly/colly/v2" ) func main() { c := colly.NewCollector() c.SetLogger(&colly.Logger{ LogFunc: func(level colly.LogLevel, msg string) { // 跳过指定日志内容,其余按原格式输出 if msg != "Max depth limit reached" { log.Printf("[%s] %s", level, msg) } }, }) // 你的爬虫逻辑... }彻底关闭Colly默认日志
如果不需要Colly的任何默认日志输出,可以直接设置空的日志处理函数:c := colly.NewCollector() c.SetLogger(&colly.Logger{ LogFunc: func(level colly.LogLevel, msg string) {}, })
内容的提问来源于stack exchange,提问作者Farshad
相关产品推荐
相关产品推荐

