2016-11-07 40 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