2016-11-07 159 views
1

我正在使用org.apache.log4j.Logger并且想要关闭htmlunit的调试消息。我做的没有任何事情似乎有效。我不断收到Http.wire,Http.headers等调试消息。我有根记录器设置为DEBUG不能拒绝HtmlUnit日志记录

我试图把这个线在我的代码:

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 

任何帮助,将不胜感激。

编辑16年11月15日(添加测试代码)

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

回答

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 
0

对于log4j的,你应该设置log4j.logger.com.gargoylesoftware.htmlunitERROR

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

感谢您的帮助。 log4j.logger.com.gargoylesoftware.htmlunit =错误在属性文件中什么也不做。代码片段可以工作,但会关闭根记录器的调试日志。我已经尝试了SimpleLog,并且Log4JLogger没有运气。我无法理解为什么我的log4j.properties文件中的设置无法为不同的记录器设置不同的级别。我更新了我正在使用的log4j.properties文件的原始问题。 – zappullae

+0

BTW。我有与Authorize.net日志记录html线细节相同的问题。 – zappullae

+0

在对此进行实验之后,似乎只使用根记录器。设置org.apache.commons.logging.Log和com.gargoylesoftware.html单元的级别和属性不起作用。唯一可行的是,如果我在log4j.properties文件中设置了一个级别,或者我做了一个Logger.getRootLogger()。setLevel(Level.WARN)。这两种解决方案都会影响所有日志记录,而不仅仅是我想要控制的个别日志。这不可能很难做到,我错过了什么? – zappullae