Spring - 使用Log4J进行日志记录

这是Spring应用程序中非常易于使用的Log4J功能.下面的示例将指导您解释Log4J和Spring之间的简单集成.

我们假设您的计算机上已经安装了 log4J .如果您没有,那么您可以从 https://logging.apache.org/并简单地在任何文件夹中提取压缩文件.我们将在项目中仅使用 log4j-xyzjar .

接下来,让我们使用一个可用的Eclipse IDE,并采取以下步骤开发动态使用Spring Web Framework的基于表单的Web应用程序 :

步骤描述
1创建一个名为 SpringExample 的项目,并在 src 文件夹下创建一个包 com.it1352在创建的项目中.
2添加必填项使用添加外部JAR 选项的Spring库,如 Spring Hello World示例章节中所述.
3使用 Add External在项目中添加log4j库 log4j-xyzjar JARs .
4创建Java类 HelloWo com.it1352包下的rld MainApp .
5 src 文件夹下创建Beans配置文件 Beans.xml .
6创建log4J配置文件 log4j. src 文件夹下的属性.
7最后一步是创建所有Java文件和Bean配置文件的内容并运行应用程序,如下所述.

以下是 HelloWorld.java 文件的内容

package com.it1352; 
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.it1352; 
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("Going to create HelloWord Obj");
      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
      obj.getMessage();
      
      log.info("Exiting the program");
   }
}

您可以生成 debug 错误消息与我们生成信息消息的方式类似.现在让我们看一下 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.IT屋.HelloWorld">
      <property name = "message" value = "Hello World!"/>
   </bean>

</beans>

以下是 log4j.properties 的内容,它定义了Log4J生成日志消息所需的标准规则

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
# Set the name of the file
log4j.appender.FILE.File=C:\\log.out

# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true

# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug

# Set the append to false, overwrite
log4j.appender.FILE.Append=false

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

完成创建源和bean配置文件后,让我们运行申请.如果您的应用程序一切正常,这将在Eclipse控制台中打印以下消息 :

Your Message : Hello World!

如果您检查C:\\驱动器,那么您应该找到包含各种日志的日志文件 log.out 消息,如下所示 :

<!-- initialization log messages -->

Going to create HelloWord Obj
Returning cached instance of singleton bean 'helloWorld'
Exiting the program

Jakarta Commons Logging(JCL)API

或者,您可以使用 Jakarta Commons Logging(JCL) API在Spring应用程序中生成日志.可以从 https://jakarta.apache.org/commons/logging/下载JCL.我们技术上需要的唯一文件是 commons-logging-xyzjar 文件,它需要以与放置 log4j-xyzjar相似的方式放在类路径中在上面的示例中.

要使用日志记录功能,您需要一个 org.apache.commons.logging.Log 对象,然后您可以调用根据您的要求,以下方法之一 :

  • 致命(对象消息)

  • 错误(对象消息)

  • 警告(对象消息)

  • info(对象消息)

  • debug(对象消息)

  • trace(对象消息)

以下是MainApp.java的替换,它使用了JCL API

package com.it1352; 
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-xyzjar文件运行程序.

现在在上面的例子中保持其余的配置和内容不变,如果你编译并运行你的应用程序,你将获得与你使用的相似的结果Log4J API.