关闭 HtmlUnit 警告

发布于 2024-09-16 16:44:30 字数 36 浏览 8 评论 0原文

您知道如何关闭 HtmlUnit 中的警告、注释和错误吗?

Do you know how can I turn Warnings, Notes, Errors in HtmlUnit off?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

东北女汉子 2024-09-23 16:44:30

将其放在代码开头的某个位置;它会闭上它的脏嘴:

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

Put this somewhere around the start of your code; it will shut its dirty mouth:

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);
简美 2024-09-23 16:44:30

Arsen Zahray 的回答中的代码有助于删除 HtmlUnit 生成的几乎所有日志。

但一项编辑有助于将它们全部删除。使用:

java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF); 

代替:

java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF);

The code in Arsen Zahray's answer helped in removing almost all the logs generated by HtmlUnit.

But one edit helps to remove them all. Use:

java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF); 

instead of:

java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF);
野心澎湃 2024-09-23 16:44:30

要从最新版本的 HtmlUnit 中删除所有输出,您只需在静态块或主类中添加这些行:

java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF); 
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");

不需要像其他答案所述那样重写任何方法。

To remove all output from the latest version of HtmlUnit you just have to add these lines in a static block or in your main class:

java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF); 
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");

It is NOT needed to override any method as some other answers state.

不知在何时 2024-09-23 16:44:30

尝试使用以下代码将日志记录级别关闭:

java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);

Try the following code to turn the logging level down to off:

java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
故人的歌 2024-09-23 16:44:30

您可以在此处获取有关如何操作 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

Here you can get info on how to manipulate logging of HtmlUnit.

This is what I added to my log4j.properties in order to disable verbose debugging messages from HtmlUnit components:

# 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
一绘本一梦想 2024-09-23 16:44:30

我正在使用下面的代码并且它运行得很好:

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

I am using the code below and it works perfectly:

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);
最近可好 2024-09-23 16:44:30

只需将此字符串添加到您的 log4.properties 中即可:

log4j.logger.com.gargoylesoftware.htmlunit=fatal

Just add this string to your log4.properties:

log4j.logger.com.gargoylesoftware.htmlunit=fatal
流年里的时光 2024-09-23 16:44:30

关闭记录器。但这不是一个好的解决方案,因为您可能希望在日志中出现一些不常见的问题。

我知道 HtmlUnit 会产生很多不重要的异常、警告等。您可以使用以下方法抑制其中的一些:

client.getOptions().setThrowExceptionOnFailingStatusCode(false);
client.getOptions().setThrowExceptionOnScriptError(false);
client.getOptions().setPrintContentOnFailingStatusCode(false);

Turn the loggers off. But that is not a good solution, since you might want to have some uncommon issues in the logs.

I know HtmlUnit produces a lot of unimportant exceptions, warnings, etc. You can suppress of few of those using:

client.getOptions().setThrowExceptionOnFailingStatusCode(false);
client.getOptions().setThrowExceptionOnScriptError(false);
client.getOptions().setPrintContentOnFailingStatusCode(false);
抠脚大汉 2024-09-23 16:44:30

现在在 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;
}

Now in HtmlUnit 2.9, WebClient.setCssErrorHandler(new SilentCssErrorHandler()) can conveniently ignore the warnings and errors. For example:

@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;
}
旧街凉风 2024-09-23 16:44:30

查看文档

有一个测试套件使用的示例 log4 文件,您可以找到它 此处,您可以根据需要禁用所有内容。

Have a look at the docs.

There is a sample log4 file used by the test suite, you can find it here, you can disable everything if you wish.

无所谓啦 2024-09-23 16:44:30

这对我有用:

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

This worked for me:

@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");
夏末 2024-09-23 16:44:30

尝试将其添加到您的代码中:

LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");

基本上,这会使记录器记录到 NoOpLog,它不会将日志信息写入任何地方。

Try adding this to your code:

LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");

Basically, this makes the logger log to NoOpLog, which doesn't write the log information anywhere.

风苍溪 2024-09-23 16:44:30

我一定在做一些与上面每个人不同的事情。我目前已将 htmlunit 设置为 Spring 项目,并删除需要将 logback.xml 添加到我的资源目录的日志。将以下内容作为 logback.xml 添加到您的 main/java/resources 目录 - 这只会输出 INFO 级别的日志语句,而不输出以下任何内容 (< a href="https://stackoverflow.com/questions/2031163/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>

I must be doing something different to everyone above. I have htmlunit set up as a Spring project currently and removing the logs required adding a logback.xml to my resources dir. Add the following as logback.xml to your main/java/resources dir - this will only output INFO level log statements and nothing below (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>
蓝戈者 2024-09-23 16:44:30

对我来说效果很好的一个选项是更改 HtmlUnit 记录器以记录到不同的文件,这样我就可以在需要时参考它的情况下出现这些错误,并且它也不会弄乱我的主日志。
以下是我对 log4j.properties 所做的 log4j 更改:

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

One option which worked well for me is to change the HtmlUnit logger to log to a different file just so that I have those errors in case I need to refer it some time and it also doesn't clutter up my main logs.
Below is the log4j change I made to 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
尤怨 2024-09-23 16:44:30

如果您不需要 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。

If you don't need JavaScript support, it is the easiest way to disable it:

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

You also disable exceptions and log on failing status codes, JavaScript, CSS errors and HTML parse errors.

If you need JavaScript support you can use a custom implementation for JavaScript errors:

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

If you don't need you also can just disable it:

client.getOptions().setCssEnabled(false);

So there is no need to configure any other Logger.

打小就很酷 2024-09-23 16:44:30

我正在使用 spring-boot,只能通过以下方式解决:

import com.gargoylesoftware.htmlunit.SilentCssErrorHandler;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.javascript.SilentJavaScriptErrorListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HtmlUnitConfiguration {

    @Bean
    public WebClient webClient() {
        WebClient webClient = new WebClient();
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.setJavaScriptErrorListener(new SilentJavaScriptErrorListener());
        webClient.setCssErrorHandler(new SilentCssErrorHandler());

        return webClient;
    }
}

然后使用 @Autowired 或在类构造函数中调用 bean。如果没有这一行:

webClient.getOptions().setThrowExceptionOnScriptError(false);

它下面的两行将抛出一个奇怪的错误。这条线有魔力。

I am using spring-boot, only solved with this:

import com.gargoylesoftware.htmlunit.SilentCssErrorHandler;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.javascript.SilentJavaScriptErrorListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HtmlUnitConfiguration {

    @Bean
    public WebClient webClient() {
        WebClient webClient = new WebClient();
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.setJavaScriptErrorListener(new SilentJavaScriptErrorListener());
        webClient.setCssErrorHandler(new SilentCssErrorHandler());

        return webClient;
    }
}

And then calling the bean with @Autowired or in class constructor. And without this line:

webClient.getOptions().setThrowExceptionOnScriptError(false);

The two lines under it will throw a bizarre error. This line has the magic.

天赋异禀 2024-09-23 16:44:30

我为 logback 用户添加这个答案:

<!-- LOG "com.gargoylesoftware.htmlunit.javascript*" at OFF (closed) level -->
    <logger name="com.gargoylesoftware.htmlunit.javascript"
        level="OFF" additivity="false">
        <appender-ref ref="RollingFile" />
        <appender-ref ref="Console" />
    </logger>

将记录器级别设置为关闭,因为 apache 日志记录和 java util 日志记录的先前答案对我不起作用(至少当我使用 htmlunit 在本地计算机上运行测试时)。

并且将虚拟侦听器作为 WebClient 实例的 javascriptErrorListener 传递对我来说也不起作用。

所以它是关闭烦人的 javascript 异常日志的唯一解决方案。

I'm adding this answer for logback users:

<!-- LOG "com.gargoylesoftware.htmlunit.javascript*" at OFF (closed) level -->
    <logger name="com.gargoylesoftware.htmlunit.javascript"
        level="OFF" additivity="false">
        <appender-ref ref="RollingFile" />
        <appender-ref ref="Console" />
    </logger>

Setting the logger level off as previous answers from apache logging and java util logging did not work for me (at least when im running tests on my local computer using htmlunit).

And passing the dummy listener as javascriptErrorListener for WebClient instance also was not working for me.

So it was the only solution for closing the annoying javascript exception logs.

战皆罪 2024-09-23 16:44:30

试试这个,它对我有用。

WebClient webClient = new WebClient();
webClient.getOptions().setJavaScriptEnabled(false);
webClient.getOptions().setCssEnabled(false);

Try this, it worked for me.

WebClient webClient = new WebClient();
webClient.getOptions().setJavaScriptEnabled(false);
webClient.getOptions().setCssEnabled(false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文