开发log4j配置_Spring 使用 Log4J 记录日志
在 Spring 應(yīng)用程序中使用 Log4J 的功能是非常容易的。下面的例子將帶你通過簡單的步驟解釋 Log4J 和 Spring 之間的簡單集成。
假設(shè)你已經(jīng)在你的機(jī)器上安裝了 Log4J,如果你還沒有 Log4J,你可以從 http://logging.apache.org/ 中下載,并且僅僅在任何文件夾中提取壓縮文件。在我們的項目中,我們將只使用 log4j-x.y.z.jar。
接下來,我們讓 Eclipse IDE 在恰當(dāng)?shù)奈恢霉ぷ?#xff0c;遵循以下步驟,使用 Spring Web 框架開發(fā)一個基于 Web 應(yīng)用程序的動態(tài)表單:
這個是 HelloWorld.java 文件的內(nèi)容:
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 的內(nèi)容:
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"); }}使用與我們已經(jīng)生成信息消息類似的方法,你可以生成調(diào)試和錯誤消息。現(xiàn)在讓我們看看 Beans.xml 文件的內(nèi)容:
<?xml version="1.0" encoding="UTF-8"?>下面是 log4j.properties 的內(nèi)容,它定義了使用 Log4J 生成日志信息所需的標(biāo)準(zhǔn)規(guī)則:
# Define the root logger with appender filelog4j.rootLogger = DEBUG, FILE# Define the file appenderlog4j.appender.FILE=org.apache.log4j.FileAppender# Set the name of the filelog4j.appender.FILE.File=C:log.out# Set the immediate flush to true (default)log4j.appender.FILE.ImmediateFlush=true# Set the threshold to debug modelog4j.appender.FILE.Threshold=debug# Set the append to false, overwritelog4j.appender.FILE.Append=false# Define the layout for file appenderlog4j.appender.FILE.layout=org.apache.log4j.PatternLayoutlog4j.appender.FILE.layout.conversionPattern=%m%n一旦你完成了創(chuàng)建源和 bean 的配置文件后,我們就可以運行該應(yīng)用程序。如果你的應(yīng)用程序一切都正常,在 Eclipse 控制臺將輸出以下信息:
Your Message : Hello World!同時如果你檢查你的 C: 驅(qū)動,那么你應(yīng)該發(fā)現(xiàn)含有各種日志消息的日志文件 log.out,其中一些如下所示:
Going to create HelloWord ObjReturning cached instance of singleton bean 'helloWorld'Exiting the programJakarta Commons Logging (JCL) API
或者,你可以使用 Jakarta Commons Logging(JCL) API 在你的 Spring 應(yīng)用程序中生成日志。JCL 可以從 http://jakarta.apache.org/commons/logging/ 下載。我們在技術(shù)上需要這個包的唯一文件是 commons-logging-x.y.z.jar 文件,需要使用與上面的例子中你使用 log4j-x.y.z.jar 類似的方法來把 commons-logging-x.y.z.jar 放在你的類路徑中。
為了使用日志功能,你需要一個 org.apache.commons.logging.Log 對象,然后你可以根據(jù)你的需要調(diào)用任何一個下面的方法:
- 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"); }}你應(yīng)該確保在編譯和運行該程序之前在你的項目中已經(jīng)引入了 commons-logging-x.y.z.jar 文件。
現(xiàn)在保持在上面的例子中剩下的配置和內(nèi)容不變,如果你編譯并運行你的應(yīng)用程序,你就會得到與使用 Log4J API 后獲得的結(jié)果類似的結(jié)果。
總結(jié)
以上是生活随笔為你收集整理的开发log4j配置_Spring 使用 Log4J 记录日志的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 交友个性签名大全
- 下一篇: 上海欢乐谷老年人免票吗