你应该在Go网络爬虫中的哪里添加日志记录?

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

Where should I add logging in my Go web scraper?

问题

我正在编写一套网站爬虫函数。每个函数都会读取一个HTML文档并返回一个单一的值。为了将所有这些函数联系在一起,我有一个函数 - 让我们称之为ScrapeUrl,它接受并读取一个URL,然后根据套件中每个爬虫函数的结果构建一个结构实例。

我想要为此添加日志记录,以便在爬虫函数返回非关键值时能够看到。但是我不知道日志记录器应该放在哪里 - 我应该从以下哪个位置记录:

  1. 在每个爬虫函数内部?
  2. 基于ScrapeUrl函数的返回值,在ScrapeUrl函数内部记录?

我有一种感觉是第二种情况,但我对Go提供的全局日志记录器不太熟悉。相反,我习惯使用具名日志记录器。

谢谢。

英文:

I'm writing a suite of website scraper functions. Each function reads an HTML document and returns a single value. To tie this all together, I've got a function - let's call it ScrapeUrl that accepts and reads a URL, then builds a struct instance out of the results from each of the scraper functions in the suite.

I want to add logging to this so that I can see when non-critical values from the scraper functions are missing. But I don't know where the logger would slide in - should I log from:

  1. Inside each scraper function?
  2. Inside the ScrapeUrl function, based on the return value?

I have a feeling it's #2 but I'm not familiar w/ global loggers like what Go offers. Instead, I'm used to named loggers.

Thanks

答案1

得分: 1

你可以使用一个命名的日志记录器,比如github.com/golang/glog,来记录两者,但只在需要时输出你想要的内容。

英文:

You can use a named logger such as github.com/golang/glog to log both, but only output what you want when you need it.

huangapple
  • 本文由 发表于 2015年8月2日 05:27:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/31766387.html
匿名

发表评论

匿名网友

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

确定