Turning HtmlUnit Warnings off

2019-01-03 09:02发布

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

13条回答
放我归山
2楼-- · 2019-01-03 09:28

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
查看更多
淡お忘
3楼-- · 2019-01-03 09:35

Arsen.. thanks a lot for the answer .. Using your code helped in removing almost all the logs 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); 
查看更多
贪生不怕死
4楼-- · 2019-01-03 09:35

Try adding this to your code :

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

Basically, logger logs to NoOpLog which doesn't write log to error or file or anywhere.

查看更多
聊天终结者
5楼-- · 2019-01-03 09:36

now in htmlunit 2.9,WebClient.setCssErrorHandler(new SilentCssErrorHandler()) can conveniently ignore the warnings and errors.

@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;
}
查看更多
成全新的幸福
6楼-- · 2019-01-03 09:39

I am using below code and it prefect.

 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);
查看更多
家丑人穷心不美
7楼-- · 2019-01-03 09:40

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.

查看更多
登录 后发表回答