servlet中使用和配置log4j
阿新 • • 發佈:2019-01-02
1、引入jar包
org.apache.log4j.FileAppender(檔案),
org.apache.log4j.DailyRollingFileAppender(每天產生一個日誌檔案),
org.apache.log4j.RollingFileAppender(檔案大小到達指定尺寸的時候產生一個新的檔案),
org.apache.log4j.WriterAppender(將日誌資訊以流格式傳送到任意指定的地方)
2、在web.xml配置檔案中做如下配置
<servlet> <servlet-name>Log4jInitServlte</servlet-name> <servlet-class>com.zdys.Servlte.Log4jInitServlte</servlet-class> <init-param> <param-name>log4j</param-name> <param-value>WEB-INF/log4j.properties</param-value><!--log4j配置檔案路徑--> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Log4jInitServlte</servlet-name> <url-pattern>/Log4jInitServlte</url-pattern> </servlet-mapping>
3.在web-INF下建立log4j.properties配置檔案 配置檔案內容如下
- 其語法為:
- log4j.rootLogger = [ level ] , appenderName1, appenderName2, …
- level: 是日誌記錄的優先順序,分為OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL或
-
者您定義的級別。Log4j建議只使用四個級別,優 先級從高到低分別是ERROR、WARN、INFO、DEBUG。通過在這裡定義的級別,您可以控制到應用程式中相應級別的日誌資訊的開關。比如在這裡定 義了INFO級別,則應用程式中所有DEBUG級別的日誌資訊將不被打印出來。
- appenderName: 就是指定日誌資訊輸出到哪個地方。您可以同時指定多個輸出目的地。
- 例如:log4j.rootLogger=info,A1,B2,C3
Appender 為日誌輸出目的地,Log4j提供的appender有以下幾種:
org.apache.log4j.FileAppender(檔案),
org.apache.log4j.DailyRollingFileAppender(每天產生一個日誌檔案),
org.apache.log4j.RollingFileAppender(檔案大小到達指定尺寸的時候產生一個新的檔案),
log4j.rootLogger=DEBUG,A1,A2,A3,A4,A6
### system.out ###
log4j.appender.A2 =org.apache.log4j.ConsoleAppender
log4j.appender.A2.Threshold=DEBUG
log4j.appender.A2.Target=System.out
log4j.appender.A2.encoding=UTF-8
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=[%x] [%-d{yyyy-MM-dd HH:mm:ss}] [%p] %m%n
### file debug ###
log4j.appender.A1 = org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.File = ../logs/DServerDebug.log
log4j.appender.A1.DatePattern='.'yyyy-MM-dd
###log4j.appender.A1.MaxFileSize=100MB
log4j.appender.A1.encoding=UTF-8
log4j.appender.A1.Append = true
log4j.appender.A1.Threshold = DEBUG
log4j.appender.A1.layout = org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=[%x] [%-d{yyyy-MM-dd HH:mm:ss}] [%p] %m%n
### file INFO ###
log4j.appender.A3 = org.apache.log4j.DailyRollingFileAppender
log4j.appender.A3.File = ../logs/DServerINFO.log
log4j.appender.A3.DatePattern='.'yyyy-MM-dd
###log4j.appender.A3.MaxFileSize=10MB
log4j.appender.A3.encoding=UTF-8
log4j.appender.A3.Append = true
log4j.appender.A3.Threshold = INFO
log4j.appender.A3.layout = org.apache.log4j.PatternLayout
log4j.appender.A3.layout.ConversionPattern=[%x] [%-d{yyyy-MM-dd HH:mm:ss}] [%p] %m%n
### file WARN ###
log4j.appender.A3 = org.apache.log4j.DailyRollingFileAppender
log4j.appender.A3.File = ../logs/DServerWARN.log
log4j.appender.A3.DatePattern='.'yyyy-MM-dd
###log4j.appender.A3.MaxFileSize=10MB
log4j.appender.A3.encoding=UTF-8
log4j.appender.A3.Append = true
log4j.appender.A3.Threshold = WARN
log4j.appender.A3.layout = org.apache.log4j.PatternLayout
log4j.appender.A3.layout.ConversionPattern=[%x] [%-d{yyyy-MM-dd HH:mm:ss}] [%p] %m%n
### file ERROR ###
log4j.appender.A4 = org.apache.log4j.DailyRollingFileAppender
log4j.appender.A4.File = ../logs/DServerERROR.log
log4j.appender.A4.DatePattern='.'yyyy-MM-dd
###log4j.appender.A4.MaxFileSize=100MB
log4j.appender.A4.encoding=UTF-8
log4j.appender.A4.Append = true
log4j.appender.A4.Threshold = ERROR
log4j.appender.A4.layout = org.apache.log4j.PatternLayout
log4j.appender.A4.layout.ConversionPattern=[%x] [%-d{yyyy-MM-dd HH:mm:ss}] [%p] %m%n
# everyday
log4j.appender.A6=org.apache.log4j.DaliyRollingFileAppender
log4j.appender.A6.File=../logs/DServerEveryDay.log
log4j.appender.A6.DatePattern=yyyyMMdd-HH'.log'
log4j.appender.A6.layout=org.apache.log4j.PatternLayout
log4j.appender.A6.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} [INFO] %m%n
4、建立Log4jInitServlte類
package com.zdys.Servlte;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.PropertyConfigurator;
public class Log4jInitServlte extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = -865926669490098235L;
public Log4jInitServlte() {
super();
}
/**
* 初始化Servlet
*/
public void init() throws ServletException {
Logger logger = Logger.getLogger(Log4jInitServlte.class);
String path = getServletContext().getRealPath("/"); String log4jpropfile = path + getInitParameter("log4j"); System.out.println("日誌的配置檔案路徑為:"+log4jpropfile); if(log4jpropfile != null) { PropertyConfigurator.configure(log4jpropfile); //載入配置檔案
System.out.println("日誌的配置檔案讀取成功"); }
logger.fatal("這是一條從TestServlet產生的fatal資訊!");
logger.error("這是一條從TestServlet產生的error資訊!");
logger.warn("這是一條從TestServlet產生的warn資訊!");
logger.debug("這是一條從TestServlet產生的debug資訊!");
logger.info("這是一條從TestServlet產生的info資訊!");
}
/**
* doGet
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* doPost
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");//設定字元編碼
response.setContentType("text/html;charset=utf-8");
}
/**
* 銷燬Servlet
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}
}