您知道如何关闭HtmlUnit中的警告,注释,错误吗?
答案 0 :(得分:100)
将它放在代码开头的某处;它将关闭它的脏嘴:
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);
webClient = new WebClient(bv);
webClient.setCssEnabled(false);
webClient.setIncorrectnessListener(new IncorrectnessListener() {
@Override
public void notify(String arg0, Object arg1) {
// TODO Auto-generated method stub
}
});
webClient.setCssErrorHandler(new ErrorHandler() {
@Override
public void warning(CSSParseException exception) throws CSSException {
// TODO Auto-generated method stub
}
@Override
public void fatalError(CSSParseException exception) throws CSSException {
// TODO Auto-generated method stub
}
@Override
public void error(CSSParseException exception) throws CSSException {
// TODO Auto-generated method stub
}
});
webClient.setJavaScriptErrorListener(new JavaScriptErrorListener() {
@Override
public void timeoutError(HtmlPage arg0, long arg1, long arg2) {
// TODO Auto-generated method stub
}
@Override
public void scriptException(HtmlPage arg0, ScriptException arg1) {
// TODO Auto-generated method stub
}
@Override
public void malformedScriptURL(HtmlPage arg0, String arg1, MalformedURLException arg2) {
// TODO Auto-generated method stub
}
@Override
public void loadScriptError(HtmlPage arg0, URL arg1, Exception arg2) {
// TODO Auto-generated method stub
}
});
webClient.setHTMLParserListener(new HTMLParserListener() {
@Override
public void warning(String arg0, URL arg1, int arg2, int arg3, String arg4) {
// TODO Auto-generated method stub
}
@Override
public void error(String arg0, URL arg1, int arg2, int arg3, String arg4) {
// TODO Auto-generated method stub
}
});
webClient.setThrowExceptionOnFailingStatusCode(false);
webClient.setThrowExceptionOnScriptError(false);
答案 1 :(得分:45)
Arsen Zahray's answer中的代码帮助删除了几乎所有由HtmlUnit生成的日志。
但是一个编辑有助于将它们全部删除。使用:
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
而不是:
java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF);
答案 2 :(得分:35)
要从最新版本的HtmlUnit中删除所有输出,您只需在静态块或主类中添加这些行:
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
不需要覆盖任何方法,因为其他一些答案陈述。
答案 3 :(得分:13)
您可以在此处获取有关how to manipulate logging of HtmlUnit的信息。
这是我添加到log4j.properties
以禁用来自HtmlUnit组件的详细调试消息的内容:
# Set specific logger levels.
log4j.logger.org.mortbay.log=fatal
log4j.logger.org.apache.http=fatal
log4j.logger.org.apache.http.headers=fatal
log4j.logger.org.apache.http.wire=fatal
# For HttpClient 3, which is used by FirefoxDriver
log4j.logger.httpclient.wire=fatal
log4j.logger.org.apache.commons=fatal
log4j.logger.com.gargoylesoftware.htmlunit=fatal
log4j.logger.com.gargoylesoftware.htmlunit.WebTestCase=fatal
# Change this to TRACE when enabling the debugger.
log4j.logger.com.gargoylesoftware.htmlunit.javascript.DebugFrameImpl=fatal
答案 4 :(得分:12)
尝试使用以下代码将日志记录级别关闭为关闭:
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
答案 5 :(得分:6)
我正在使用下面的代码,它完美无缺:
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);
答案 6 :(得分:3)
关闭记录仪。但这不是一个好的解决方案,因为您可能希望在日志中出现一些不常见的问题。
我知道HtmlUnit会产生很多不重要的异常,警告等等。你可以抑制少数几个使用:
client.getOptions().setThrowExceptionOnFailingStatusCode(false);
client.getOptions().setThrowExceptionOnScriptError(false);
client.getOptions().setPrintContentOnFailingStatusCode(false);
答案 7 :(得分:3)
只需将此字符串添加到log4.properties
:
log4j.logger.com.gargoylesoftware.htmlunit=fatal
答案 8 :(得分:2)
答案 9 :(得分:2)
这对我有用:
@Test
public void homePage() throws Exception {
final WebClient webClient = new WebClient();
webClient.setThrowExceptionOnScriptError(false);
final HtmlPage page = webClient.getPage("http://localhost:8080/web/guest/home");
答案 10 :(得分:1)
尝试将此添加到您的代码中:
LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
基本上,这会使记录器记录到NoOpLog
,而不会在任何地方写入日志信息。
答案 11 :(得分:1)
现在在HtmlUnit 2.9中,WebClient.setCssErrorHandler(new SilentCssErrorHandler())
可以方便地忽略警告和错误。例如:
@Override
protected WebClient modifyWebClient(WebClient client) {
// currently does nothing, but may be changed in future versions
WebClient modifiedClient = super.modifyWebClient(client);
modifiedClient.getOptions().setThrowExceptionOnScriptError(false);
modifiedClient.setCssErrorHandler(new SilentCssErrorHandler());
return modifiedClient;
}
答案 12 :(得分:1)
我必须做与上面每个人不同的事情。我目前已将htmlunit设置为Spring项目,并删除了在资源目录中添加logback.xml
所需的日志。将以下内容作为logback.xml
添加到您的main/java/resources
目录中-这只会输出INFO
级日志语句,而不会在下面的内容{When to use the different log levels)
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!--<include resource="org/springframework/boot/logging/logback/base.xml"/>-->
<!--<logger name="org.springframework.web" level="INFO"/>-->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] [%logger{20}] %-5level - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>gdaxDesktop.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %logger{20} %-5level - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<!--<appender-ref ref="FILE"/>-->
<appender-ref ref="CONSOLE"/>
</root>
</configuration>
答案 13 :(得分:0)
对我来说效果很好的一个选项是更改HtmlUnit记录器以记录到另一个文件,以便我有这些错误,以防我需要引用它一段时间,它也不会使我的主日志混乱。
以下是我对log4j
所做的log4j.properties
更改:
log4j.logger.com.gargoylesoftware.htmlunit=ERROR, HTMLUNIT
log4j.additivity.com.gargoylesoftware.htmlunit=false
log4j.appender.HTMLUNIT = org.apache.log4j.RollingFileAppender
log4j.appender.HTMLUNIT.layout=org.apache.log4j.PatternLayout
log4j.appender.HTMLUNIT.layout.ConversionPattern=%m%n
log4j.appender.HTMLUNIT.File=logs/HtmlUnitLog4j.log
log4j.appender.HTMLUNIT.MaxFileSize=5MB
log4j.appender.HTMLUNIT.MaxBackupIndex=5
答案 14 :(得分:0)
如果不需要JavaScript支持,这是禁用它的最简单方法:
WebClient client = new WebClient(BrowserVersion.BEST_SUPPORTED);
client.getOptions().setThrowExceptionOnFailingStatusCode(false);
client.getOptions().setPrintContentOnFailingStatusCode(false);
client.getOptions().setThrowExceptionOnScriptError(false);
client.getOptions().setJavaScriptEnabled(false);
client.setCssErrorHandler(new SilentCssErrorHandler());
client.setHTMLParserListener(new HTMLParserListener() {
@Override
public void error(String message, URL url, String html, int line, int column, String key) {
}
@Override
public void warning(String message, URL url, String html, int line, int column, String key) {
}
});
您还可以禁用异常并登录失败的状态代码,JavaScript,CSS错误和HTML解析错误。
如果您需要JavaScript支持,则可以针对JavaScript错误使用自定义实现:
client.setJavaScriptErrorListener(new JavaScriptErrorListener() {
@Override
public void timeoutError(HtmlPage arg0, long arg1, long arg2) {
}
@Override
public void scriptException(HtmlPage arg0, ScriptException arg1) {
}
@Override
public void malformedScriptURL(HtmlPage arg0, String arg1, MalformedURLException arg2) {
}
@Override
public void loadScriptError(HtmlPage arg0, URL arg1, Exception arg2) {
}
});
如果您不需要,也可以将其禁用:
client.getOptions().setCssEnabled(false);
因此不需要配置任何其他Logger。
答案 15 :(得分:0)
试试这个,它对我有用。
WebClient webClient = new WebClient();
webClient.getOptions().setJavaScriptEnabled(false);
webClient.getOptions().setCssEnabled(false);