java – 使用org.apache.commons.logging编写日志文件

我正在写一个应用程序,我需要使用org.apache.commons.logging库将日志写入文件,但是我不知道如何开始.

有人可以帮我吗

谢谢&最好的祝福.

解决方法

尝试这个样本,首先你需要两个这样的属性文件;

commons-logging.properties放入应用程序的类路径.该文件内容应如下所示:

org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger

您还可以使用Log4j logger除了Jdk14Logger.And需要第二个自定义属性文件.例如log-config.properties如下所示:

# The following creates two handlers
    handlers=java.util.logging.ConsoleHandler,java.util.logging.FileHandler
    # Set the default logging level for the root logger
    .level=SEVERE
    # log level for the "com.example" package
    sample.logging.level=FINE
    # Set the default logging level
    java.util.logging.ConsoleHandler.level=ALL
    java.util.logging.FileHandler.level=FINE
    # Set the default formatter
    java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
    java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
    # Specify the location and name of the log file
    java.util.logging.FileHandler.pattern=D:/temp/log/test.log

这是样本测试类

public class TestLog {

     private static Log log = LogFactory.getLog(TestLog.class);
     public static void main(String[] args) {
          log.info("Testing Info Message.");
              if (log.isDebugEnabled()) {
                  log.debug("Testing Debug Message.");
          }
        }
     }

这是使用eclipse的示例包结构;

添加TestLog类的编辑配置在VM参数下这样;

-Djava.util.logging.config.file=/D:/dev/workspace/LoggingTest/bin/log-config.properties(your properties file path)

然后运行,您可以在D:/temp/log/test.log下找到您的日志文件

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...