我正在使用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");
}
感谢您的帮助。 log4j.logger.com.gargoylesoftware.htmlunit =错误在属性文件中什么也不做。代码片段可以工作,但会关闭根记录器的调试日志。我已经尝试了SimpleLog,并且Log4JLogger没有运气。我无法理解为什么我的log4j.properties文件中的设置无法为不同的记录器设置不同的级别。我更新了我正在使用的log4j.properties文件的原始问题。 – zappullae
BTW。我有与Authorize.net日志记录html线细节相同的问题。 – zappullae
在对此进行实验之后,似乎只使用根记录器。设置org.apache.commons.logging.Log和com.gargoylesoftware.html单元的级别和属性不起作用。唯一可行的是,如果我在log4j.properties文件中设置了一个级别,或者我做了一个Logger.getRootLogger()。setLevel(Level.WARN)。这两种解决方案都会影响所有日志记录,而不仅仅是我想要控制的个别日志。这不可能很难做到,我错过了什么? – zappullae