Can't turn off HtmlUnit logging messages
Asked Answered
A

7

16

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?

Annalisaannalise answered 3/3, 2011 at 23:52 Comment(1)
Possible duplicate of Turning HtmlUnit Warnings offFall
M
15

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

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.

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

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

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

log4j.logger.com.gargoylesoftware.htmlunit=ERROR
Morton answered 26/4, 2011 at 17:4 Comment(0)
S
11

You can also use:

org.apache.log4j.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(org.apache.log4j.Level.FATAL);
java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(java.util.logging.Level.SEVERE);
Shaving answered 26/11, 2012 at 2:18 Comment(1)
For me that's the only things among the high number of tests done which clear the console..... It's almost peacefulNeutrality
M
8

Try putting this in your code:

LogManager.getLogManager().reset();
Mcgarry answered 21/1, 2012 at 10:23 Comment(0)
S
3

Add this into your code:

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);    
java.util.logging.Logger.getLogger("org.apache.http.client.protocol.ResponseProcessCookies").setLevel(Level.OFF);

The last line will turn off most of the logs that is happening due to the Cookies getting accepted or rejected at the target site.

Shiprigged answered 22/5, 2014 at 18:57 Comment(0)
L
2

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);
Laic answered 23/12, 2013 at 8:30 Comment(0)
W
1

Try by writing your own log4j config file, and making htmlunit use it with the java option:

-Dlog4j.configuration=file:///my/conf/log4j.properties

Then in your log4j.properties:

log4j.logger.com.gargoylesoftware.htmlunit=error
Wonderful answered 2/3, 2012 at 14:24 Comment(0)
G
0

If using Spring, spring uses LogBack by default so you can create a logback.xml or logback-test.xml in 'main/resources/' or 'test/resources' folder.

You can set log level by package like this:

<configuration>
    <include resource="/org/springframework/boot/logging/logback/base.xml"/>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>
    <root level="error">
        <appender-ref ref="STDOUT"/>
    </root>
    <logger name="com.mypackage" level="debug"/>
    <logger name="com.gargoylesoftware" level="error"/>
</configuration>
Gleich answered 3/9, 2019 at 20:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.