目录

Spring - Logging with Log4J

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

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

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

脚步 描述
1 创建一个名为SpringExample的项目,并在创建的项目中的src文件夹下创建一个包com.iowiki
2 使用Add External JARs选项添加所需的Spring库,如Spring Hello World Example章节中所述。
3 使用Add External JARs在项目中添加log4j库log4j-xyzjar
4com.iowiki包下创建Java类HelloWorldMainApp
5src文件夹下创建Beans配置文件Beans.xml
6src文件夹下创建log4J配置文件log4j.properties
7 最后一步是创建所有Java文件和Bean配置文件的内容并运行应用程序,如下所述。

这是HelloWorld.java文件的内容

package com.iowiki;
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.iowiki;
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");
   }
}

您可以采用与生成信息消息类似的方式生成debugerror消息。 现在让我们看看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.iowiki.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对象,然后您可以根据您的要求调用以下方法之一 -

  • fatal(Object message)
  • 错误(对象消息)
  • warn(Object message)
  • info(Object message)
  • debug(Object message)
  • trace(Object message)

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

package com.iowiki;
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相似的结果。

↑回到顶部↑
WIKI教程 @2018