1、下載commons-logging-1.1.1-bin.zip文件,解壓之後找到文件:commons-logging-1.1.1.jar
2、在Eclipse中建立一個Web工程,並配置能在Tomcat 5下運行
3、把第1步中的commons-logging-1.1.1.jar文件拷貝到Web工程的WEB-INF/lib文件夾下
4、在src文件夾建立commons-logging配置文件:commons-logging.properties;這個文件在編譯之後會自動拷貝到WEB-INF/classes文件夾下,此文件的內容爲:
org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog
這個設置表明使用commons-logging中自帶的日誌實現類SimpleLog來記錄日誌,如果採用其它的類,比如說log4j,這可以通過修改此處來改變
5、在src文件夾下建立SimpleLog配置文件:simplelog.properties;這個文件在編譯之後會自動拷貝到WEB-INF/classes文件夾下,此文件的內容爲:
org.apache.commons.logging.simplelog.defaultlog=trace
org.apache.commons.logging.simplelog.log.XXXXXX=debug
org.apache.commons.logging.simplelog.showlogname=true
org.apache.commons.logging.simplelog.showShortLogname=true
org.apache.commons.logging.simplelog.showdatetime=true
org.apache.commons.logging.simplelog.dateTimeFormat=yyyy-MM-dd hh:mm:ss
其中最主要的兩個設置爲:org.apache.commons.logging.simplelog.defaultlog和org.apache.commons.logging.simplelog.log.XXXXXX(此處的XXXXXX根據實際情況填寫),這兩個設置項值都可以是trace、
debug、
info、
warn、
error、
fatal中的一個;org.apache.commons.logging.simplelog.defaultlog設置所有log類的日誌級別,而org.apache.commons.logging.simplelog.log.XXXXXX則針對具體實例中的log類設置日誌級別,若實例中的日誌級別沒有設置,則默認按defaultlog的設置執行;若以上兩項都沒有設置的話,則會默認是info日誌級別。
6、在WebRoot下建立文件index.jsp文件,具體內容如下:
<%@ page language="java" import="org.apache.commons.logging.Log" pageEncoding="utf-8"%>
<%@ page language="java" import="org.apache.commons.logging.LogFactory" %>
<%
Log log=LogFactory.getLog(this.getClass().getName());
log.info(this.getClass().getName());//獲取log所在的實例類名
if (log.isTraceEnabled()) {
log.trace("index.jsp Trace Inner");
}
if(log.isDebugEnabled()){
log.debug("index.jsp Debug Inner");
}
if(log.isInfoEnabled()){
log.info("index.jsp Info Inner");
}
if(log.isWarnEnabled()){
log.warn("index.jsp Warn Inner");
}
if(log.isErrorEnabled()){
log.error("index.jsp Error Inner");
}
if(log.isFatalEnabled()){
log.fatal("index.jsp Error Inner");
}
log.trace("index.jsp Trace Outer");
log.debug("index.jsp Debug Outer");
log.info("index.jsp Info Outer");
log.warn("index.jsp Warn Outer");
log.error("index.jsp Error Outer");
log.fatal("index.jsp Fatal Outer");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
This is my JSP page. <br>
</body>
</html>
7、運行程序,訪問index.jsp,然後查看Console的記錄,會發現所有的日誌都顯示了,然後查看jsp頁面產生的類名,此處爲:org.apache.jsp.index_jsp
8、修改配置文件simplelog.properties,把其中的XXXXXX替換爲org.apache.jsp.index_jsp,並設置其爲error,具體如下:
org.apache.commons.logging.simplelog.defaultlog=trace
org.apache.commons.logging.simplelog.log.org.apache.jsp.index_jsp=error
org.apache.commons.logging.simplelog.showlogname=true
org.apache.commons.logging.simplelog.showShortLogname=true
org.apache.commons.logging.simplelog.showdatetime=true
org.apache.commons.logging.simplelog.dateTimeFormat=yyyy-MM-dd hh:mm:ss
9、重新運行程序,訪問index.jsp頁面,查看其日誌顯示情況,只會顯示error和fatal級別的日誌
參考鏈接:
SimpleLog配置文件設置
http://commons.apache.org/logging/apidocs/org/apache/commons/logging/impl/SimpleLog.html
http://commons.apache.org/logging/guide.html#Creating%20a%20Log%20Implementation
commons-logging-1.1.1基本使用和配置
http://blog.csdn.net/gtuu0123/article/details/4509884
JSP頁面的DEBUG
http://www.tutorialspoint.com/jsp/jsp_debugging.htm