Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turn Off Apache Common Logging

Tags:

java

I am using Apache Common Logging library in my standalone application. After searching through the web, I try to turn off the logging by using

package javaapplication1;  import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory;  /**  *  * @author yccheok  */ public class Main {      /**      * @param args the command line arguments      */     public static void main(String[] args) {         // TODO code application logic here         System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");          log.info("You do not want to see me");     }      private static final Log log = LogFactory.getLog(Main.class); } 

However, I still can see the log message being printed. May I know what had I missed out?

I can turn off the logging by putting

# Sample ResourceBundle properties file org.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLog 

in commons-logging.properties.

However, during my development time, my Netbeans doesn't know where to get commons-logging.properties, and sometimes I need to turn off logging during development time.

like image 260
Cheok Yan Cheng Avatar asked Sep 17 '09 04:09

Cheok Yan Cheng


People also ask

Does Apache Commons Logging use log4j?

Apache Commons Logging is an abstraction for the concrete implementation. It uses log4j, if present and configured.

What is org Apache Commons Logging?

Apache Commons Logging (previously known as Jakarta Commons Logging or JCL) is a Java-based logging utility and a programming model for logging and for other toolkits. It provides APIs, log implementations, and wrapper implementations over some other tools.

How do I set classpath for Apache Commons Logging?

Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Common Collections jar location. Assuming, you have stored commons-collections4-4.1-bin. zip in Apache folder on various Operating Systems as follows. export CLASSPATH=$CLASSPATH:$APACHE_HOME/commons-collections4-4.1-bin.


1 Answers

As others have pointed out, this is happening because you create the Log object before you set the property.

One way around this would be to set the property in your Main class' static initialiser block - this will be run when the class is first loaded, and before the static final Log is created:

public class Main {     static {       System.setProperty("org.apache.commons.logging.Log",                          "org.apache.commons.logging.impl.NoOpLog");    }     // Rest of class as before } 
like image 141
Andrzej Doyle Avatar answered Oct 02 '22 11:10

Andrzej Doyle