【发布时间】:2017-03-21 05:11:06
【问题描述】:
我正在使用 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");
}
【问题讨论】:
标签: log4j htmlunit apache-commons-logging