关闭Apache Common Logging [英] Turn Off Apache Common Logging

查看:585
本文介绍了关闭Apache Common Logging的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在独立应用程序中使用Apache Common Logging库。在网上搜索之后,我尝试使用

  package javaapplication1;关闭日志记录。 

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/ **
*
* @author yccheok
* /
公共类主要{

/ **
* @param args命令行参数
* /
public static void main(String [] args){
// TODO代码应用程序逻辑这里
System.setProperty (org.apache.commons.logging.Log,org.apache.commons.logging.impl.NoOpLog);

log.info(你不想见我);
}

private static final Log log = LogFactory.getLog(Main.class);
}

但是,我仍然可以看到正在打印的日志消息。我可以知道我错过了什么吗?



我可以通过点击

来关闭日志记录 #Sample ResourceBundle属性文件
org.apache.commons.logging.Log = org.apache.commons.logging.impl.NoOpLog




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

解决方案

正如其他人指出的那样out,这种情况正在发生,因为您在设置属性之前>创建了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 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.

解决方案

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆