Java:无法关闭HtmlUnit日志消息(2016)

时间:2016-09-29 08:17:48

标签: java logging htmlunit

我无法关闭HtmlUnit日志消息。

我知道,之前已经解决了这个问题:

我在这些帖子中找到了所有答案,但没有一个能为我工作。

这是我的配置:

LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
java.util.logging.Logger.getLogger("org.apache.commons.httpclient").setLevel(Level.OFF);
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
org.apache.log4j.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(org.apache.log4j.Level.OFF);
java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.OFF);
java.util.logging.Logger.getLogger("logging.level.org.springframework.web").setLevel(java.util.logging.Level.OFF);
org.apache.log4j.Logger.getLogger("org.apache.http.client.protocol.ResponseProcessCookies").setLevel(org.apache.log4j.Level.OFF);
java.util.logging.Logger.getLogger("org.apache.http.client.protocol.ResponseProcessCookies").setLevel(java.util.logging.Level.OFF);
java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter").setLevel(Level.OFF);
java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit.javascript.host.ActiveXObject").setLevel(Level.OFF);
java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument").setLevel(Level.OFF);
java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit.html.HtmlScript").setLevel(Level.OFF);
java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit.javascript.host.WindowProxy").setLevel(Level.OFF);
java.util.logging.Logger.getLogger("org.apache").setLevel(Level.OFF);

WebClient client = new WebClient(BROWSER);
client.getOptions().setTimeout((int) TimeUnit.SECONDS.toMillis(30));
client.getCookieManager().setCookiesEnabled(true);
client.getOptions().setJavaScriptEnabled(true);
client.getOptions().setUseInsecureSSL(true);
client.getOptions().setCssEnabled(false);
client.getOptions().setThrowExceptionOnFailingStatusCode(false);
client.getOptions().setThrowExceptionOnScriptError(false);
client.getOptions().setPrintContentOnFailingStatusCode(false);
client.setJavaScriptErrorListener(new JavaScriptErrorListener() {

  @Override public void scriptException(InteractivePage var1, ScriptException var2) {}

  @Override public void timeoutError(InteractivePage var1, long var2, long var4) {}

  @Override public void malformedScriptURL(InteractivePage var1, String var2, MalformedURLException var3) {}

  @Override public void loadScriptError(InteractivePage var1, URL var2, Exception var3) {}
});

这是我从上面的帖子得到的总配置。当然,我尝试了每个配置独立,但每次我收到消息,如:

2016-09-29 10:06:15.765  INFO 17849 --- [pool-1-thread-2] c.g.h.javascript.JavaScriptEngine        : Caught script exception

com.gargoylesoftware.htmlunit.ScriptException: Error:   
[$injector:modulerr] Failed to instantiate module sk.teng due to:
[$injector:modulerr] Failed to instantiate module sk.shared due to:
[$injector:unpr] Unknown provider: gettextCatalogProvider
http://errors.angularjs.org/1.5.7/$injector/unpr?p0=gettextCatalogProvider
http://errors.angularjs.org/1.5.7/$injector/modulerr?p0=sk.shared&p1=%5B%24injector%3Aunpr%5D%20Unknown%20provider%3A%20gettextCatalogProvider%0Ahttp%3A%2F%2Ferrors.angularjs.org%2F1.5.7%2F%24injector%2Funpr%3Fp0%3DgettextCatalogProvider
http://errors.angularjs.org/1.5.7/$injector/modulerr?p0=sk.teng&p1=%5B%24injector%3Amodulerr%5D%20Failed%20to%20instantiate%20module%20sk.shared%20due%20to%3A%0A%5B%24injector%3Aunpr%5D%20Unknown%20provider%3A%20gettextCatalogProvider%0Ahttp%3A%2F%2Ferrors.angularjs.org%2F1.5.7%2F%24injector%2Funpr%3Fp0%3DgettextCatalogProvider%0Ahttp%3A%2F%2Ferrors.angularjs.org%2F1.5.7%2F%24injector%2Fmodulerr%3Fp0%3Dsk.shared%26p1%3D%255B%2524injector%253Aunpr%255D%2520Unknown%2520provider%253A%2520gettextCatalogProvider%250Ahttp%253A%252F%252Ferrors.angularjs.org%252F1.5.7%252F%2524injector%252Funpr%253Fp0%253DgettextCatalogProvider (https://www.sk.sk:443/js/ng-vendor.js?1474366837#4)
at     com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:894)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:628)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:513)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:825)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:797)
at com.gargoylesoftware.htmlunit.InteractivePage.executeJavaScriptFunctionIfPossible(InteractivePage.java:216)
at com.gargoylesoftware.htmlunit.javascript.host.event.EventListenersContainer.executeEventListeners(EventListenersContainer.java:258)
at com.gargoylesoftware.htmlunit.javascript.host.event.EventListenersContainer.executeBubblingListeners(EventListenersContainer.java:322)
at com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget.fireEvent(EventTarget.java:192)
at com.gargoylesoftware.htmlunit.html.DomElement$2.run(DomElement.java:1174)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:628)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:513)
at com.gargoylesoftware.htmlunit.html.DomElement.fireEvent(DomElement.java:1179)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeEventHandlersIfNeeded(HtmlPage.java:1222)
at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:253)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:519)
at com.gargoylesoftware.htmlunit.WebClient.loadDownloadedResponses(WebClient.java:2144)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.doProcessPostponedActions(JavaScriptEngine.java:922)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.processPostponedActions(JavaScriptEngine.java:1012)
at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:799)
at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:742)
at com.gargoylesoftware.htmlunit.html.DomElement.click(DomElement.java:689)
org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

有什么新方法可以阻止HtmlUnit日志记录吗?

1 个答案:

答案 0 :(得分:0)

这解决了这个问题:

  webClient.setJavaScriptErrorListener(new DummyJavascriptErrorListener());
  webClient.setIncorrectnessListener(new DummyIncorrectnessListener());

JS错误监听器

public class DummyJavascriptErrorListener implements 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

  }
}

不正确的侦听器:

public class DummyIncorrectnessListener implements IncorrectnessListener {

  @Override
  public void notify(String s, Object o) {
    // nothing to do ...
  }
}