无法拒绝HtmlUnit记录

时间:2016-11-07 18:41:55

标签: log4j htmlunit apache-commons-logging

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

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

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");
    }

2 个答案:

答案 0 :(得分:0)

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

log4j.logger.com.gargoylesoftware.htmlunit=ERROR

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

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);

答案 1 :(得分:0)

因此,经过多次试验和观察日志输出后,我已经想到了这一点。事实证明,Apache HTTPCLient website 上的HttpClient文档不正确(事实上,链接已断开)。事实证明,HTTPClient使用的记录器的名称不是doc中指定的。正确的记录器根名称为" 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