log4j的應用具體解析。本站提示廣大學習愛好者:(log4j的應用具體解析)文章只能為提供參考,不一定能成為您想要的結果。以下是log4j的應用具體解析正文
1 Log4j設置裝備擺設解釋
1.1 設置裝備擺設文件
Log4j可以經由過程java法式靜態設置,該方法顯著缺陷是:假如須要修正日記輸入級別等信息,則必需修正java文件,然後從新編譯,很是費事;
log4j也能夠經由過程設置裝備擺設文件的方法停止設置,今朝支撐兩種格局的設置裝備擺設文件:
•xml文件
•properties文件(推舉)
上面是一個log4j設置裝備擺設文件的完全內容:
log4j.rootCategory=INFO, stdout
log4j.rootLogger=info, stdout
### stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p - %m%n
### set package ###
log4j.logger.org.springframework=info
log4j.logger.org.apache.catalina=info
log4j.logger.org.apache.commons.digester.Digester=info
log4j.logger.org.apache.catalina.startup.TldConfig=info
log4j.logger.chb.test=debug
1.2 設置裝備擺設根Logger
根logger重要界說log4j支撐的日記級別及輸入目標地,其語法為:
log4j.rootLogger = [ level ] , appenderName, appenderName, …
個中,level 是日記記載的優先級,分為OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL或許自界說的級別。
建議只應用四個級別,優先級從高到低分離是ERROR、WARN、INFO、DEBUG。
appenderName指定日記信息輸入到哪一個處所,可同時指定多個輸入目標地。
1.3 設置裝備擺設輸入目標地Appender
Appender重要界說日記信息輸入在甚麼地位,重要語法為:
log4j.appender.appenderName = classInfo
log4j.appender.appenderName.option1 = value1
…
log4j.appender.appenderName.optionN = valueN
Log4j供給的appender有以下幾種:
•org.apache.log4j.ConsoleAppender(掌握台),
•org.apache.log4j.FileAppender(文件),
•org.apache.log4j.DailyRollingFileAppender(天天發生一個日記文件),
•org.apache.log4j.RollingFileAppender(文件年夜小達到指定尺寸的時刻發生一個新的文件)
•org.apache.log4j.WriterAppender(將日記信息以流格局發送就任意指定的處所)
以ConsoleAppender為例,如:
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
1.4 設置裝備擺設日記信息的格局Layout
Layout 擔任格局化Appender的輸入,其語法為:
log4j.appender.appenderName.layout = classInfo
log4j.appender.appenderName.layout.option1 = value1
…
log4j.appender.appenderName.layout.optionN = valueN
個中,Log4j供給的layout有以下幾種:
•org.apache.log4j.HTMLLayout(以HTML表格情勢結構),
•org.apache.log4j.PatternLayout(可以靈巧地指定結構形式),
•org.apache.log4j.SimpleLayout(包括日記信息的級別和信息字符串)
•org.apache.log4j.TTCCLayout(包括日記發生的時光、線程、種別等等信息)
1.5 設置package輸入級別
可以設置分歧package的日記輸入級別,語法為:
log4j.logger.packageName=level
個中,packageName為現實的包名,level為日記級別,例如:
log4j.logger.org.springframework=info
log4j.logger.org.apache.catalina=info
log4j.logger.org.apache.commons.digester.Digester=info
log4j.logger.org.apache.catalina.startup.TldConfig=info
log4j.logger.chb.test=debug
2 Log4j與J2ee聯合
2.1 應用spring架構
Spring真是不錯,替我們做了許多工作,假如體系應用了spring框架,則要集成log4j就很簡略了,重要分為3個步調,以下:
2.1.1 界說log4j設置裝備擺設文件
log4j.rootCategory=INFO, stdout
log4j.rootLogger=info, stdout
### stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p - %m%n
### log to file ###
log4j.logger.org.springframework=info
log4j.logger.org.apache.catalina=info
log4j.logger.org.apache.commons.digester.Digester=info
log4j.logger.org.apache.catalina.startup.TldConfig=info
log4j.logger.chb.test=debug
2.1.2 界說監聽器
監聽器須要界說在web.xml,重要包含:界說log4j設置裝備擺設文件目次、log4j監聽器,以下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--由Spring載入的Log4j設置裝備擺設文件地位-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext*.xml
</param-value>
</context-param>
<!--Spring log4j Config loader-->
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>InitiaServlet</servlet-name>
<servlet-class>chb.test.web.InitiaServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2.1.3 測試類
package com.dheaven.mip.web;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.Logger;
public class InitiaServlet extends HttpServlet {
protected Logger log = Logger.getLogger(InitiaServlet.class);
private static final long serialVersionUID = 8550329576989690578L;
/**
* Constructor of the object.
*/
public InitiaServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
log.debug("辦事器啟動了,log4j開端任務了");
}
}
2.2不應用spring架構
假如體系沒有應用spring,我們以servlet為例,很簡略,年夜家依照步調履行便可,只貼代碼,不說空話。
2.2.1 界說log4j設置裝備擺設文件
放在web工程的WEB-INF目次下,內容如:
log4j.rootCategory=INFO, stdout
log4j.rootLogger=info, stdout
### stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p - %m%n
### set package ###
log4j.logger.org.apache.catalina=info
log4j.logger.org.apache.commons.digester.Digester=info
log4j.logger.org.apache.catalina.startup.TldConfig=info
log4j.logger.com.dheaven=debug
2.2.2 創立log4j初始化類
package com.dheaven.mip.web;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.PropertyConfigurator;
public class InitLog4j extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init() throws ServletException {
String prefix = getServletContext().getRealPath("/");
prefix = prefix.replace("//", "/");
String file = getInitParameter("log4j-init-file");
// if the log4j-init-file is not set, then no point in trying
if (file != null) {
PropertyConfigurator.configure(prefix + file);
}
}
}
2.2.3 在Web.xml中界說初始化類
<servlet>
<servlet-name>log4j-init</servlet-name>
<servlet-class>chb.test.web.InitLog4j</servlet-class>
<init-param>
<param-name>log4j-init-file</param-name>
<param-value>WEB-INF/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
2.2.4 測試類
package chb.test.web;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.Logger;
public class InitiaServlet extends HttpServlet {
protected Logger log = Logger.getLogger(InitiaServlet.class);
private static final long serialVersionUID = 8550329576989690578L;
/**
* Constructor of the object.
*/
public InitiaServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
log.debug("辦事器啟動了,log4j開端任務了");
}
}