无法关闭 HtmlUnit 日志记录

Can't turn down HtmlUnit logging

我正在使用 org.apache.log4j.Logger 并想关闭 htmlunit 的调试消息。我所做的一切似乎都不起作用。我不断收到 Http.wire、Http.headers 等调试消息。我将根记录器设置为 debug

我尝试将这一行放在我的代码中:

org.apache.log4j.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(org.apache.log4j.Level.OFF);

我也试过将这一行放在我的 log4j.properties 文件中:

log4j.logger.com.gargoylesoftware.htmlunit=WARN

这是我的 log4j.properties 文件的内容:

log4j.logger.com.gargoylesoftware.htmlunit=ERROR

log4j.logger.org.apache.commons.httpclient=ERROR


# Tell the root lodger what appenders and level to use
log4j.rootLogger=DEBUG, A1, A2


##### Console Appender #####

log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n


##### File Appender #####

log4j.appender.A2=org.apache.log4j.FileAppender
log4j.appender.A2.File=/var/log/mylogfile.log
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n
log4j.appender.A2.Append=false

如有任何帮助,我们将不胜感激。

编辑 11/15/16(添加测试代码)

import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.*;

import org.junit.*;
import static org.junit.Assert.*;

import org.apache.commons.logging.*;
import org.apache.log4j.*;

public class Test01
{
    @Test
    public void homePage() throws Exception
    {
        LogFactory.getFactory().setAttribute("com.gargoylesoftware.htmlunit", "org.apache.commons.logging.impl.Log4JLogger");
        LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Log4JLogger");


        org.apache.log4j.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.WARN);
        org.apache.log4j.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.WARN); 

        Logger.getRootLogger().setLevel(Level.DEBUG);


        Logger.getRootLogger().debug("Start");

        WebClient webClient = new WebClient()

        HtmlPage page = webClient.getPage("https://google.com");

        Logger.getRootLogger().debug("End");
    }

对于 log4j,您应该将 log4j.logger.com.gargoylesoftware.htmlunit 设置为 ERROR:

log4j.logger.com.gargoylesoftware.htmlunit=ERROR

第二个选项:在您的代码中,在声明您的网络客户端后添加:

LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");

java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF); 
java.util.logging.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.OFF);

经过多次试验和观察日志输出后,我发现了这一点。事实证明,Apache HTTPCLient website 上的 HttpClient 文档不正确(事实上存在断开的链接)。事实证明 HTTPClient 使用的记录器的名称与文档中指定的不一样。正确的记录器根名称是 "http" 而不是 "httpclient" 这意味着我执行的所有试验都没有效果。

我正在使用 org.apache.httpcomponents.httpclient_4.5.2 和 org.apache.httpcomponents.httpcore_4.4.5,截至今天 (11/15/16).

这是一个示例 log4j.properties 文件,它将允许对 HTMLClient 日志记录进行精细控制

# Tell the root logger what appenders and level to use
log4j.rootLogger=DEBUG, A1, A2


# Controls detailed wire protocol
log4j.logger.org.apache.http.wire=WARN

# Controls headers (good for debugging)
log4j.logger.org.apache.http.headers=WARN

# Controls http context (what you are sending and geting)
log4j.logger.org.apache.http=WARN

# Controls htmlunit details
log4j.logger.com.gargoylesoftware.htmlunit=WARN


##### Console Appender #####

log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n


##### File Appender #####

log4j.appender.A2=org.apache.log4j.FileAppender
log4j.appender.A2.File=mylogfile.log
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n
log4j.appender.A2.Append=false