1 Log4j配置說明java
1.1 配置文件
Log4j能夠經過java程序動態設置,該方式明顯缺點是:若是須要修改日誌輸出級別等信息,則必須修改java文件,而後從新編譯,非常麻煩;web
log4j也能夠經過配置文件的方式進行設置,目前支持兩種格式的配置文件:spring
•xml文件
•properties文件(推薦)
下面是一個log4j配置文件的完整內容:apache
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主要定義日誌信息輸出在什麼位置,主要語法爲:app
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爲例,如:jsp
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開始工做了"); } }