Java 关闭 Apache 公共日志记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1436761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:14:59  来源:igfitidea点击:

Turn Off Apache Common Logging

java

提问by Cheok Yan Cheng

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

我在我的独立应用程序中使用 Apache Common Logging 库。通过网络搜索后,我尝试使用关闭日志记录

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.

在 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.

但是,在我的开发期间,我的 Netbeans 不知道从哪里获取 commons-logging.properties,有时我需要在开发期间关闭日志记录。

采纳答案by Andrzej Doyle

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

正如其他人指出的那样,发生这种情况是因为您设置属性之前创建了 Log 对象。

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

解决此问题的一种方法是在Main类的静态初始化块中设置属性- 这将在类首次加载时运行,并在创建静态最终日志之前运行:

public class Main {

   static {
      System.setProperty("org.apache.commons.logging.Log",
                         "org.apache.commons.logging.impl.NoOpLog");
   }

   // Rest of class as before
}

回答by Thorbj?rn Ravn Andersen

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

一种不同的方法可能是 - 在开发时 - 使用 slf4j 项目来控制日志记录。

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.

使用 commonds 日志记录桥用 sljf4 替换 Apache COMmons 日志记录,然后使用合适的日志记录后端。例如,slf4j simple 或 NOP 实现对您来说是合理的。这些只是您放入类路径中的几个 jar。

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

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

回答by leonm

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:

您的示例的问题是在设置属性之前实例化了 Log 类。如果在设置属性后创建 Log 实例,则示例正常工作。例如,如果将其移动到 main 方法中:

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");
    }

}

回答by Peter ?tibrany

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

正如其他人指出的那样,您的log实例是在设置系统属性之前实例化的,这为时过早。

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

尝试传递-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLog给您的 JVM 以确保它在正确的时间设置。