无法关闭HtmlUnit日志记录消息 [英] Can't turn off HtmlUnit logging messages

查看:623
本文介绍了无法关闭HtmlUnit日志记录消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HtmlUnit与通过Ajax与服务器交互的网页进行交互. Ajax代码启动后不久,HtmlUnit会生成以下两条日志消息:

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

我不知道如何关闭这些消息.遍历代码表明,它们是由直接调用记录器而不是通过处理程序对象产生的,而我可以为CSS错误做这件事,而该处理程序对象我可以为此提供不做任何事情的实现. HtmlUnit日志记录页面指示添加以下代码应该可以:

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

...但是没有效果.我还尝试在调用Java的地方添加以下选项:

...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.

我想我可以在执行此代码的同时将stderr重定向到/dev/null,但是还可以使用其他一些较黑的解决方案吗?

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

推荐答案

我对此也有疑问. 答案取决于引擎盖下使用的是哪种日志记录系统commons-logging. (因为common-logging只是一个包装器).见以下 http://commons.apache.org/proper/commons-logging/guide.html#Configuring_The_Underlying_Logging_System

I too had issues with this.. The answer depends on what logging system commons-logging is using under the hood. (since common-logging is just a wrapper). See the following http://commons.apache.org/proper/commons-logging/guide.html#Configuring_The_Underlying_Logging_System

您上面提到的属性(org.apache.commons.logging.simplelog.defaultlog)仅在使用简单记录器的情况下才有效.如果您在JDK 1.4或更高版本上运行,则应默认使用JDK日志记录.在这种情况下,默认情况下是从JRE安装位置使用lib/logging.properties.

The attribute you mention above (org.apache.commons.logging.simplelog.defaultlog) should only be valid if the simple logger is been used. If you are running on JDK 1.4 or higher however it should default to using the JDK logging. In which case it defaults to using the lib/logging.properties from the JRE install location.

在我的情况下,我在类路径中有Log4j,所以它默认为Log4j.

In my case I had Log4j in the classpath, so it defaulted to that.

要消除所有这些随机性,您可以自己明确设置Logger.在类路径中创建commons-logging.properties文件,并通过记录器以使用例如

To take away the randomness of all this you can explicitly set the Logger yourself. Create a commons-logging.properties file in the classpath and pass in the logger to use e.g.

# JDK Logging
#org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger
# Log4j logging (also required log4j.jar to be in classpath)
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

对于log4j,添加以下log4j.properties可停止来自HtmlUnit的警告.

For log4j, adding the following the log4j.properties stops the warnings from HtmlUnit.

log4j.logger.com.gargoylesoftware.htmlunit=ERROR

这篇关于无法关闭HtmlUnit日志记录消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆