Disabling HtmlUnit Warnings

Do you know how I can turn off warnings, notes, errors in HtmlUnit?

+56
warnings htmlunit
Aug 30 '10 at 13:01
source share
14 answers

Put it somewhere at the beginning of your code; he will shut his 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); 
+100
Aug 26 2018-11-11T00:
source share

The code in Arsen Zahrey’s answer helped remove almost all of the logs generated by HtmlUnit.

But one edit helps to delete them all. Using:

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

instead:

 java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF); 
+45
Jul 26 2018-12-12T00:
source share

To remove all output from the latest version of HtmlUnit, you just need 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 required to override any method, as some other answers claim.

+33
Sep 01 '13 at 22:29
source share

Here you can get information on how to manipulate HtmlUnit logging .

This is what I added to my log4j.properties to disconnect 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 
+13
Oct 28 '12 at 17:15
source share

Try the following code to turn off the logging level:

 java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF); 
+12
Oct 22 '12 at 10:53 on
source share

I use the code below and it works great:

 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
Feb 12 '15 at 18:53
source share

Turn off the recorders. But this is not a good solution, as you may want to have some unusual issues in the logs.

I know that HtmlUnit generates many unimportant exceptions, warnings, etc. You can suppress a few of those who use:

 client.getOptions().setThrowExceptionOnFailingStatusCode(false); client.getOptions().setThrowExceptionOnScriptError(false); client.getOptions().setPrintContentOnFailingStatusCode(false); 
+3
Nov 03 '13 at 13:32
source share

Just add this line to your log4.properties :

 log4j.logger.com.gargoylesoftware.htmlunit=fatal 
+3
Jun 21 '14 at 21:50
source share

Check out the docs .

There is an example log4 file that is used by the test suite, you can find it here , you can disable everything if you want.

+2
Aug 31 2018-10-10T00:
source share

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"); 
+2
Nov 06 '12 at 11:28
source share

Try adding this to your code:

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

Essentially, this makes a log in NoOpLog , which does not record log information anywhere.

+1
May 13 '13 at 5:12
source share

Now in HtmlUnit 2.9 WebClient.setCssErrorHandler(new SilentCssErrorHandler()) can easily ignore 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; } 
+1
Dec 22 '15 at 3:57
source share

One option that came in handy for me is to change the HtmlUnit logger to register in another file so that I have these errors in case I need to access it for a while, and this also does not clutter up my main logs. Below log4j.properties I log4j.properties 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 
0
Dec 14 '16 at 10:40
source share

If you don't need JavaScript support, this 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 report error status codes, JavaScript errors, CSS errors, and HTML parsing errors.

If you need JavaScript support, you can use your own 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 do not need it, you can also just disable it:

 client.getOptions().setCssEnabled(false); 

Thus, there is no need to configure any other Logger.

0
Apr 12 '19 at 22:22
source share



All Articles