Turning HtmlUnit Warnings off

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


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

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

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.


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

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

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