📜  Spring-使用Log4J记录

📅  最后修改于: 2020-11-11 07:09:07             🧑  作者: Mango


这是Spring应用程序中非常易于使用的Log4J功能。下面的示例将带您通过简单的步骤来解释Log4J和Spring之间的简单集成。

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

接下来,让我们准备好运行的Eclipse IDE,并采取以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序-

Steps Description
1 Create a project with a name SpringExample and create a package com.tutorialspoint under the src folder in the created project.
2 Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.
3 Add log4j library log4j-x.y.z.jar as well in your project using using Add External JARs.
4 Create Java classes HelloWorld and MainApp under the com.tutorialspoint package.
5 Create Beans configuration file Beans.xml under the src folder.
6 Create log4J configuration file log4j.properties under the src folder.
7 The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.

这是HelloWorld.java文件的内容

package com.tutorialspoint;

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.tutorialspoint;

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");
   }
}

您可以通过类似于生成信息消息的方式来生成调试错误消息。现在让我们看看Beans.xml文件的内容





   
      
   


以下是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 ,如下所示-



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(对象消息)
  • 调试(对象消息)
  • 跟踪(对象消息)

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

package com.tutorialspoint;

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相似的结果。