微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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下找到您的日志文件

原文地址:https://www.jb51.cc/java/120629.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐