使用org.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的示例包结构;
并在VM参数下添加TestLog类的Edit Configuration就像这样;
-Djava.util.logging.config.file=/D:/dev/workspace/LoggingTest/bin/log-config.properties(your properties file path)
然后运行,您可以在D:/temp/log/test.log下找到您的日志文件
我希望这有帮助…这就是我们在项目中的表现……
A.在项目中包含jar。
B.为loggers定义log4j.xml这样的定义……
C.在课堂上使用记录器:
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; Class YourClass{ private static Log log = LogFactory.getLog(YourClass.class); public void yourMethod(){ log.info("Your Message"); } }
编辑:D。因为我们有一个JBoss AS环境所以应用程序配置为读取log4j.xml如下(你需要一个等效的配置):
resource:jboss-log4j.xml true 60