Can't turn off HtmlUnit logging messages

2020-02-20 05:24发布

I'm using HtmlUnit to interact with a web page that interacts with the server via Ajax. Soon after the Ajax code starts, HtmlUnit produces these two log messages:

WARNING: Ignoring XMLHttpRequest.setRequestHeader for Content-length: it is a restricted header
Mar 3, 2011 3:32:47 PM com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest jsxFunction_setRequestHeader
WARNING: Ignoring XMLHttpRequest.setRequestHeader for Connection: it is a restricted header
Mar 3, 2011 3:32:47 PM com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest jsxGet_status

...Followed by this message, repeated six times:

SEVERE: XMLHttpRequest.status was retrieved before the response was available.
Mar 3, 2011 3:32:47 PM com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest jsxGet_status

I can't figure out how to turn these messages off. Trawling through the code shows that they're produced by direct calls to a logger, not via a handler object that I could provide a do-nothing implementation for, as I already do for CSS errors. The HtmlUnit logging page indicates that adding this code should work:

System.getProperties().put("org.apache.commons.logging.simplelog.defaultlog", "fatal");

...but it has no effect. I also tried adding the following option where I invoke java:

-Dorg.apache.commons.logging.simplelog.defaultlog=fatal

...but that also has no effect.

I guess I could redirect stderr to /dev/null while this code is executing, but are any less hacky solutions available?

7条回答
forever°为你锁心
2楼-- · 2020-02-20 06:11

I am using Htmlunit 2.13, and following code is working to trunoff warnings.

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);
查看更多
登录 后发表回答