转:http://blog.csdn.net/gxmark/article/details/7338253
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