Turning HtmlUnit Warnings off
Asked Answered
L

18

66

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

Leifeste answered 30/8, 2010 at 13:1 Comment(0)
H
105

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);
Hereabouts answered 26/8, 2011 at 6:33 Comment(6)
HtmlUnit provides SilentCssErrorHandler so you don't have to make an empty anonymous inner class for setCssErrorHandler(ErrorHandler).Audwen
I didn't have to use all of these. Just using java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF); worked for me.Amble
It's worth mentioning that on some versions you must call getOptions() on webClient first to set the throw exception options in this example, like this: webClient.getOptions().setThrowExceptionOnScriptError(false);Crosier
even with this I still get "RHINO USAGE WARNING" anyway to prevent rhino warnings?Whitehot
still receiving warnings: "Script is not JavaScript" from c.g.htmlunit.html.HtmlScriptKopeisk
Can you update the answer for the latest versionLalita
N
53

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);
Natality answered 26/7, 2012 at 15:12 Comment(1)
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF);Quadriplegic
F
41

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.

Fastness answered 1/9, 2013 at 22:29 Comment(2)
This works perfectly, the first answer above is not working anymore.Eliseoelish
Did not work for me. HtmlUnit 2.26. I had to update my log4j.properties with: log4j.logger.com.gargoylesoftware=errorKrueger
H
14

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

java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
Hovel answered 22/10, 2012 at 10:53 Comment(0)
R
12

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
Ransack answered 28/10, 2012 at 17:15 Comment(1)
Worked for me, and doesn't turn off logging completely which is useful for me since I want to view the fatal errors.Risteau
R
6

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);
Ricebird answered 12/2, 2015 at 18:53 Comment(0)
D
3

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);
Daisie answered 3/11, 2013 at 13:32 Comment(0)
M
3

Just add this string to your log4.properties:

log4j.logger.com.gargoylesoftware.htmlunit=fatal
Mediative answered 21/6, 2014 at 21:50 Comment(0)
K
3

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;
}
Kasandrakasevich answered 22/12, 2015 at 3:57 Comment(0)
H
2

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.

Houk answered 31/8, 2010 at 2:4 Comment(0)
E
2

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");
Eberhart answered 6/11, 2012 at 11:28 Comment(0)
I
1

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.

Invulnerable answered 13/5, 2013 at 5:12 Comment(0)
T
1

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>
Thanksgiving answered 14/7, 2020 at 23:7 Comment(1)
Same case here, using latest version of Spring Boot. Nice tip.Illene
R
0

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
Risteau answered 14/12, 2016 at 22:40 Comment(0)
M
0

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.

Mai answered 12/4, 2019 at 22:22 Comment(0)
C
0

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.

Charron answered 4/1, 2022 at 2:56 Comment(0)
K
0

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.

Kathline answered 24/12, 2022 at 20:23 Comment(0)
A
-1

Try this, it worked for me.

WebClient webClient = new WebClient();
webClient.getOptions().setJavaScriptEnabled(false);
webClient.getOptions().setCssEnabled(false);
Appetency answered 6/1, 2021 at 1:12 Comment(1)
this is disabling the CSS and Javascript on the page. It is not what intented.Disquietude

© 2022 - 2024 — McMap. All rights reserved.