Spring - 使用 Log4J 进行日志记录
这是 Spring 应用程序中一个非常易于使用的 Log4J 功能。下面的示例将通过简单步骤带您了解 Log4J 与 Spring 的简单集成。首先,我们需要准备一个可用的 Eclipse IDE,并按照以下步骤使用 Spring Web Framework 开发一个基于 Dynamic Form 的 Web 应用程序 −
示例 - 使用 Log4j 进行日志记录
| 步骤 | 描述 |
|---|---|
| 1 | 创建一个名为 spring 的 Maven 项目,groupid 为 com.,artifactid 为 spring,并在创建的项目中的 src 文件夹下创建一个 com. 包。 |
| 2 | 按照 Spring - 环境设置 章节的说明更新 pom.xml。添加 log4j 作为依赖,如下所示。 |
| 3 | 在 com. 包下创建 Java 类 TextEditor、SpellChecker 和 MainApp。 |
| 4 | 在 src/main/resources 文件夹下创建 Beans 配置文件 Beans.xml。添加 log4j2.properties,如下所示。 |
| 5 | 最后一步是创建所有 Java 文件和 Bean 配置文件的內容。按照以下说明运行应用程序。 |
HelloWorld.java
package com.;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage() {
System.out.println("Your Message : " + message);
}
}
MainApp.java
package com.;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.apache.log4j.Logger;
public class MainApp {
static Logger log = Logger.getLogger(MainApp.class.getName());
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
log.info("即将创建 HelloWord 对象");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
log.info("程序退出");
}
}
您可以类似地生成 debug 和 error 消息,就像我们生成 info 消息一样。现在让我们查看 Beans.xml 文件的内容。
Beans.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id = "helloWorld" class = "com..HelloWorld">
<property name = "message" value = "Hello World!"/>
</bean>
</beans>
log4j2.properties
以下是 log4j2.properties 的内容,它定义了 Log4J 生成日志消息所需的标准规则。
appender.0.type = File appender.0.name = MAIN appender.0.fileName = logs/main.log appender.0.layout.type = PatternLayout appender.0.layout.pattern = %p - %m%n rootLogger.level = INFO rootLogger.appenderRef.0.ref = MAIN
输出
完成源代码和 Bean 配置文件创建后,让我们运行应用程序。如果您的应用程序一切正常,这将在 Eclipse 控制台中打印以下消息 −
Your Message : Hello World!
如果您检查当前目录下的 logs 文件夹,那么您应该会找到日志文件 main.log,其中包含各种日志消息,例如以下内容 −
INFO - 即将创建 HelloWord 对象 INFO - 程序退出
Jakarta Commons Logging (JCL) API
或者,您可以使用 Jakarta Commons Logging (JCL) API 在您的 Spring 应用程序中生成日志。JCL 可以作为以下依赖项引入 −
<dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.3.5</version> </dependency>
从这个包中,我们技术上唯一需要的是 commons-logging-x.y.z.jar 文件,它需要以与上面示例中放置 log4j-x.y.z.jar 类似的方式放置在您的 classpath 中。
要使用日志功能,您需要一个 org.apache.commons.logging.Log 对象,然后可以根据您的需求调用以下方法之一 −
- fatal(Object message)
- error(Object message)
- warn(Object message)
- info(Object message)
- debug(Object message)
- trace(Object message)
MainApp.java
以下是 MainApp.java 的替换版本,它使用了 JCL API
package com.;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.apache.commons.logging. Log;
import org.apache.commons.logging. LogFactory;
public class MainApp {
static Log log = LogFactory.getLog(MainApp.class.getName());
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
log.info("Going to create HelloWord Obj");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
log.info("Exiting the program");
}
}
在编译和运行程序之前,您必须确保已将 commons-logging-x.y.z.jar 文件包含在您的项目中。
现在,在保持上述示例中其余配置和内容不变的情况下,如果您编译并运行应用程序,将得到与使用 Log4J API 时类似的结果。