Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

java - Turn Off Apache Common Logging

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...