35

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.

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

5 Answers5

53

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
}
Peter Štibraný
  • 32,463
  • 16
  • 90
  • 116
Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
  • Do You know how to disable org.apache.commons.logging.Log in JRuby? I'm using: `require 'java' java::lang.static { java::lang.System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog") }` but it does nothing... – user3719188 Aug 20 '15 at 12:13
8

As others pointed out, your log instance is instantiated before system property is set, which is too early.

Try passing -Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLog to your JVM to make sure it is set at the right time.

Peter Štibraný
  • 32,463
  • 16
  • 90
  • 116
6

A different approach could be - while developing - to use the slf4j project to control logging.

Using the commonds logging bridge to replace Apache COmmons logging with sljf4 and then use a suitable logging backend. E.g. the slf4j simple or NOP implementation would be reasonable for you. These are just a few jars you drop in your classpath.

See http://www.slf4j.org/legacy.html#jcl-over-slf4j

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
4

The problem with your example is that the Log class is instantiated before the property is set. If you create the Log instance after the property is set the example works properly. For example if you move it into the main method:

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 log = LogFactory.getLog(Main.class);

        log.info("You do not want to see me");
    }

}
Peter Štibraný
  • 32,463
  • 16
  • 90
  • 116
leonm
  • 6,454
  • 30
  • 38
0

There are multiple ways:

  • Shutdown the common-logging completely (this will affect all your dependencies)

    System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
    
  • Pass common-logging logs through other log platforms (example slf4j) and control all logs easily from one place. (For example, the below hack will force all common-logging logs to be managed by Slf4j)

     <!-- for log4j give a try to log4j-jcl dependency -->
     <dependency>
       <groupId>commons-logging</groupId>
       <artifactId>commons-logging</artifactId>
       <version>1.1.1</version>
       <scope>provided</scope>
     </dependency>
    
     <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>jcl-over-slf4j</artifactId>
       <version>1.7.36</version>
     </dependency>
    
  • Dependency exclusion in inheriting projects

     <exclusions>
         <exclusion>
             <groupId>commons-logging</groupId>
             <artifactId>commons-logging</artifactId>
         </exclusion>
     </exclusions>
    
Mehdi
  • 3,795
  • 3
  • 36
  • 65