views:

811

answers:

5

I am using a jar file in a java program and it generates warnings during runtime. But I don't want that my customer sees these warnings. How can I disable these warnings.

The warning is as below:

Sep 25, 2009 10:10:33 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Expected content type of 'application/javascript' or 'application/ecmascript' for remotely loaded JavaScript element at 'http://www.craigslist.org/js/jquery.js', but got 'application/x-javascript'.
A: 

Assuming these are log messages, you could configure the logger to write everything to null or /dev/null

Putting a file like this in the path might help

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"&gt;

    <appender name="NULL" class="org.apache.log4j.FileAppender">
        <param name="File" value="/dev/null"/>
    </appender>

   <logger name="com.gargoylesoftware.htmlunit">
       <level value="FATAL"/>
       <appender-ref ref="NULL"/>
   </logger>

</log4j:configuration>
sal
Can you please explain in detail. I am not using any logger.
Yatendra Goel
Then a library you're using must be.
Michael Myers
It would probably be a better idea to change the level of the logger to only log ERROR level - changing the log to write everything to dev/null means you probably lose any actual error reporting that you may need for debug later.
Nate
The library you are using, is using java.util.logging.* to output information.
Thorbjørn Ravn Andersen
Can you please tell me how to write everything to null or /dev/null
Yatendra Goel
@Yatendra - yes, you are useing a logger. Not explicitly, but your code is using one (HtmlUnit does log errors). Read the documentation, which specifically states "If you don't explicitly configure commons logging ... then it will use the simple logger." You ned to point the simple logger at /dev/null or equivalent. Read the documentation for how to do that.
DaveE
+5  A: 

From the appearance of the message, you are using HtmlUnit which uses Commons Logging for logging messages. Unless you configure Commons Logging to write to a file, the log messages will get logged by the simple logger of Commons Logging which writes out onto the console.

If you want to make the error messages go away you could adopt either of the options:

  1. Configure Commons Logging to write to a file on disk (using log4j).
  2. Redirect the console output to /dev/null or its equivalent, as sal pinpointed.
Vineet Reynolds
So I want to apply your option no. 2 i.e Redirect the console output to /dev/nullCan you please tell me how to redirect them
Yatendra Goel
/dev/null is for Unix-like OSes. Redirecting the output of a process in Unix involves the following: command > /dev/null . Replace command with your command suitably.
Vineet Reynolds
You need to learn how to configure Commons Logging. See the link Vineet included.
matt b
You might want to read up http://en.wikipedia.org/wiki/Redirection_%28computing%29
Vineet Reynolds
DaveE
@DaveE, +1. Exactly why redirection is #2 on the list. I don't the OP's customer will find it amusing when he's told that a newer version of the application, that uses log4j, needs to be deployed to track an issue.
Vineet Reynolds
DaveE
+1  A: 

Just copy first lines from the link Vineet posted:

If you don't explicitly configure commons logging to use LOG4J or another logging framework then it will use the simple logger. When using the simple logger, you can change the default logging level by setting the following system property:

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

Add this code somewhere in the beginning of the program.

tulskiy
Too bad this doesn't seem to work :-(
Roboprog
my bad: log4j is not bundled with HtmlUnit. See my "answer"
Roboprog
+1  A: 

Since the OP keeps asking how to redirect to /dev/null: You can achieve a similar effect by calling System.setOut and System.setErr, passing in a PrintStream that does nothing with the output it's given.

This is a terrible hack, and the previous answers are far far cleaner.

daveb
A: 

I found that HtmlUnit does not include the log4j implementation, only the commons logging "interface" (and abstract classes). Once you toss log4j.jar on the path, HtmlUnit behaves itself much better, and honors its configs.

Roboprog