我正在编写一个应用程序,我需要使用 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 的示例包结构;
然后在 VM 参数下添加 TestLog 类的编辑配置,如下所示;
-Djava.util.logging.config.file=/D:/dev/workspace/LoggingTest/bin/log-config.properties(your properties file path)
然后运行然后你可以在D:/temp/log/test.log下找到你的日志文件
关于java - 使用 org.apache.commons.logging 写入日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11815642/