日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

中國最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2

wkspring教程

Spring 使用 Log4J 記錄日志

閱讀 (2192)

使用 Log4J 記錄日志

在 Spring 應用程序中使用 Log4J 的功能是非常容易的。下面的例子將帶你通過簡單的步驟解釋 Log4J 和 Spring 之間的簡單集成。

假設你已經在你的機器上安裝了 Log4J,如果你還沒有 Log4J,你可以從 http://logging.apache.org/ 中下載,并且僅僅在任何文件夾中提取壓縮文件。在我們的項目中,我們將只使用 log4j-x.y.z.jar

接下來,我們讓 Eclipse IDE 在恰當的位置工作,遵循以下步驟,使用 Spring Web 框架開發一個基于 Web 應用程序的動態表單:

步驟描述
1創建一個名稱為 SpringExample 的項目,并且在創建項目的 src 文件夾中創建一個包 com.tutorialspoint
2使用 Add External JARs 選項,添加所需的 Spring 庫,解釋見 Spring Hello World Example 章節。
3使用 Add External JARs 選項,同樣在你的項目中添加 log4j 庫 log4j-x.y.z.jar
4com.tutorialspoint 包下創建 Java 類 HelloWorldMainApp
5src 文件中創建 Bean 配置文件 Beans.xml
6src 文件中創建 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");
   }
}

使用與我們已經生成信息消息類似的方法,你可以生成調試錯誤消息。現在讓我們看看 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 可以從 http://jakarta.apache.org/commons/logging/ 下載。我們在技術上需要這個包的唯一文件是 commons-logging-x.y.z.jar 文件,需要使用與上面的例子中你使用 log4j-x.y.z.jar 類似的方法來把 commons-logging-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)

下面是使用 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-x.y.z.jar 文件。

現在保持在上面的例子中剩下的配置和內容不變,如果你編譯并運行你的應用程序,你就會得到與使用 Log4J API 后獲得的結果類似的結果。

關閉
程序員人生
主站蜘蛛池模板: 偷拍亚洲 | 久久久亚洲综合 | 国产欧美日韩在线观看 | 久久男女视频 | 久久久99国产精品免费 | 亚洲一区二区中文字幕 | 中文字幕一区二区三 | 国产一区二区视频在线 | 日韩精品中文字幕一区二区 | 午夜二区| 国产5区| 伊人黄| 999re5这里只有精品 | 国产黄色一级 | 亚洲黄色免费 | 亚洲h| 日韩在线亚洲 | 精品国产一二三 | 久久久久久久久久久美女 | 午夜精品一区二区三区视频免费看 | 国产电影在线观看 | 狠狠搞狠狠干 | 在线午夜av | 久久久久国产一区二区三区四区 | 国产99久久精品一区二区永久免费 | 麻豆视频免费观看 | 国产欧美精品一区aⅴ影院 岛国av免费看 | av在线不卡一区 | 国产精品久久久久久吹潮 | 久久精品国产一区二区电影 | 美日韩一区 | 高清国产一区二区三区四区五区 | 在线观看av资源 | 999午夜| 日本在线视频一区二区三区 | 91高清免费看 | 日韩在线观看视频免费 | 精品一区二区三区在线视频 | 日韩一级 | 国产一页 | 国产一区二区成人在线 |