Spring - 使用Log4J進行日誌記錄



這是一個在Spring應用程式中非常易於使用的Log4J功能。以下示例將引導您完成簡單的步驟,以解釋Log4J和Spring之間簡單的整合。

我們假設您已經在您的機器上安裝了log4j。如果您沒有安裝,您可以從https://logging.apache.org/下載它,並將壓縮檔案解壓到任何資料夾中。在我們的專案中,我們只需要使用log4j-x.y.z.jar

接下來,讓我們準備好一個可用的Eclipse IDE,並按照以下步驟使用Spring Web框架開發一個基於動態表單的Web應用程式:

步驟 描述
1 建立一個名為SpringExample的專案,並在建立的專案中的src資料夾下建立一個包com.tutorialspoint
2 Spring HelloWorld示例章節中所述,使用新增外部JARs選項新增所需的Spring庫。
3 同樣,使用新增外部JARs選項將log4j庫log4j-x.y.z.jar新增到您的專案中。
4 com.tutorialspoint包下建立Java類HelloWorldMainApp
5 src資料夾下建立Bean配置檔案Beans.xml
6 src資料夾下建立log4J配置檔案log4j.properties
7 最後一步是建立所有Java檔案和Bean配置檔案的內容,並按如下所述執行應用程式。

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

您可以以類似於生成資訊訊息的方式生成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.tutorialspoint.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應用程式中生成日誌。JCL可以從https://jakarta.apache.org/commons/logging/下載。從這個包中,我們技術上只需要commons-logging-x.y.z.jar檔案,這個檔案需要像上面示例中放置log4j-x.y.z.jar一樣放置到您的類路徑中。

要使用日誌記錄功能,您需要一個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的替換版本,它使用了JCL API

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-x.y.z.jar檔案包含在您的專案中。

現在保持上述示例中的其餘配置和內容不變,如果您編譯並執行您的應用程式,您將獲得與使用Log4J API獲得的結果類似的結果。

廣告
© . All rights reserved.