java - 使用 org.apache.commons.logging 写入日志文件

标签 java apache-commons-logging

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

谁能帮帮我?

谢谢并致以最诚挚的问候。

最佳答案

试试这个例子,首先你需要两个像这样的属性文件;

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

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

除了 Jdk14Logger 之外,您还可以使用 Log4j 记录器。并且需要第二个自定义属性文件。例如 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 的示例包结构;

enter image description here

然后在 VM 参数下添加 TestLog 类的编辑配置,如下所示;

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

enter image description here

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

关于java - 使用 org.apache.commons.logging 写入日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11815642/

相关文章:

java - 从 Swing 文本框中获取列表

java - Android MediaPlayer - 位置在缓冲时跳过

java - JRException : java. lang.ClassCastException : java. lang.String 无法转换为 java.lang.Boolean

java - 将接口(interface)的类​​信息存储在 HashMap 中,并稍后创建该接口(interface)的实例

java - 如何让我的方法在特定日期/时间执行?

elasticsearch - 为ElasticSearch Java Rest Client的跟踪程序包禁用跟踪日志记录

java - 如何修复 ClassNotFoundException : org. apache.commons.logging.LogFactory?

java - 如何让 OpenEJB 使用 slf4j?

java - Quartz 和 Spring 的 NullPointerException

java - SLF4J,通用日志记录,控制台输出