>创建了Log对象。 解决这个问题的一种方法是设置属性您的 Main 类'静态初始化程序块 - 这将在首次加载类时以及在创建静态最终日志之前运行: public class Main { static { System.setProperty(org.apache.c ommons.logging.Log,org.apache.commons.logging.impl.NoOpLog); } //以前的其他课程} I am using Apache Common Logging library in my standalone application. After searching through the web, I try to turn off the logging by usingpackage 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 fileorg.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLogin 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. 解决方案 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} 这篇关于关闭Apache Common Logging的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-15 17:33