0

我为 3 个不同的站点编写了爬虫,并在 3 个线程中运行爬虫。对于每个爬虫,我使用单个记录器。我的“log4j.properties”文件如下所示:

log4j.rootLogger=TRACE, ZDNET, CNET, GOOGLEPLAY

log4j.appender.ZDNET=org.apache.log4j.RollingFileAppender
log4j.appender.ZDNET.File=logs/zdnet.log
log4j.appender.ZDNET.MaxFileSize=20MB
log4j.appender.ZDNET.MaxBackupIndex=100
log4j.appender.ZDNET.layout=org.apache.log4j.PatternLayout
log4j.appender.ZDNET.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n

log4j.appender.CNET=org.apache.log4j.RollingFileAppender
log4j.appender.CNET.File=logs/cnet.log
log4j.appender.CNET.MaxFileSize=20MB
log4j.appender.CNET.MaxBackupIndex=100
log4j.appender.CNET.layout=org.apache.log4j.PatternLayout
log4j.appender.CNET.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n

log4j.appender.GOOGLEPLAY=org.apache.log4j.RollingFileAppender
log4j.appender.GOOGLEPLAY.File=logs/googlePlay.log
log4j.appender.GOOGLEPLAY.MaxFileSize=20MB
log4j.appender.GOOGLEPLAY.MaxBackupIndex=100
log4j.appender.GOOGLEPLAY.layout=org.apache.log4j.PatternLayout
log4j.appender.GOOGLEPLAY.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n

log4j.category.zdNetLogger=DEBUG, ZDNET
log4j.additivity.zdNetLogger=false

log4j.category.cNetLogger=DEBUG, CNET
log4j.additivity.cNetLogger=false

log4j.category.googlePlayLogger=DEBUG, GOOGLEPLAY
log4j.additivity.googlePlayLogger=false

在java中,我使用下面的代码来写我的日志

final Logger APK_LOG = Logger.getLogger("googlePlayLogger");
final Logger C_NET_LOG = Logger.getLogger("cNetLogger");
final Logger ZD_NET_LOG = Logger.getLogger("zdNetLogger");
....
ZD_NET_LOG.info("1");
C_NET_LOG.info("2");
APK_LOG.info("3");

一切正常,直到我开始使用 Selenium + HtmlUnit + HtmlUnitDriver。之后,当我运行我的程序时,来自 HtmlUnitDriver 的日志填充了 3 个日志文件(zdnet.log、cnet.log、googlePlay.log)。之后从我的文件(zdnet.log、cnet.log、googlePlay.log)中记录信息:

2015-06-16 02:47:08 DEBUG - Get page for window named '', using WebRequest[<url="about:blank", GET, EncodingType[name=application/x-www-form-urlencoded], [], {Accept=image/gif, image/jpeg, image/pjpeg, image/pjpeg, */*, Accept-Encoding=gzip, deflate}, null>]
2015-06-16 02:47:08 DEBUG - setEnclosedPage: HtmlPage(about:blank)@945834833
2015-06-16 02:47:08 DEBUG - destroyChildren
2015-06-16 02:47:08 DEBUG - Encoding found in HTTP headers: 'UTF-8'.
2015-06-16 02:47:08 DEBUG - Mapping java.lang.Object to HTMLCollection
2015-06-16 02:47:08 DEBUG - Mapping com.gargoylesoftware.htmlunit.html.HtmlSpan to HTMLSpanElement

任何想法为什么会发生?

4

1 回答 1

0

因为HttpClientHtmlUnit都使用 log4j 来编写它们的日志。

rootLogger定义了trace级别,因此它会写入所有内容。

请添加以下内容以仅允许errorHttpClient 和 HtmlUnit 的级别消息。

log4j.logger.com.gargoylesoftware.htmlunit=error
log4j.logger.org.apache.http=error
于 2015-06-16T01:13:06.730 回答