虽然在JSP中打印日志是大家都不想使用的。但由于个别需求需要对JSP进行维护的时候使用日志打印是首选之道。
下面转自国外网站介绍在JSP中使用LOG4J的实例。
原文地址:http://www.mobilefish.com/developer/log4j/log4j_quickguide_jsp.html
Apply log4j within jsp.
Information
none
Operating system used
Windows XP Home Edition Version 5.1 SP 2
Software prerequisites
Jakarta log tag library
Log4j 1.2.9
Procedure
- Download the Jakarta log tag library: jakarta-taglibs-log-1.0.zip
- Extract this archive file, e.g.: C:\Tools\jakarta-taglibs-log-1.0
The archive contains the following files:
LICENSE
log-doc.war
log-examples.war
README
taglibs-log.jar
taglibs-log.tld
- Download Log4j 1.2.9: logging-log4j-1.2.9.zip
- Extract this archive file, e.g.: C:\Tools\logging-log4j-1.2.9
The archive contains serveral files. The most important one is: C:\Tools\logging-log4j-1.2.9\dist\lib\log4j-1.2.9.jar
- Install Tomcat. Follow guide "Installing Tomcat 4.1.31".
- Create a simple a Tomcat web application called "demo":
- Create the following directories inside the Tomcat "webapps" directory:
C:\Tools\Tomcat 4.1\webapps\demo
C:\Tools\Tomcat 4.1\webapps\demo\WEB-INF
C:\Tools\Tomcat 4.1\webapps\demo\WEB-INF\classes
C:\Tools\Tomcat 4.1\webapps\demo\WEB-INF\lib
- Copy the tag library descriptor file "taglibs-log.tld" into WEB-INF.
- Copy the tag library JAR file "taglibs-log.jar" into WEB-INF/lib.
- Copy the log4j JAR file "log4j-1.2.9.jar" into WEB-INF/lib.
- Create a web.xml file in the WEB-INF directory:
<?xml version="1.0"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<display-name>Demonstration log4j usage in jsp</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>http://jakarta.apache.org/taglibs/log-1.0</taglib-uri>
<taglib-location>/WEB-INF/taglibs-log.tld</taglib-location>
</taglib>
</web-app>
Note: You MUST add a <taglib> element in the web.xml file:
<taglib>
<taglib-uri>http://jakarta.apache.org/taglibs/log-1.0</taglib-uri>
<taglib-location>/WEB-INF/log.tld</taglib-location>
</taglib>
- Create a log4j.properties file in the WEB-INF/classes directory:
# ***** Set root logger level to DEBUG and its two appenders to stdout and R.
log4j.rootLogger=debug, stdout, R
# ***** stdout is set to be a ConsoleAppender.
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# ***** stdout uses PatternLayout.
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%c] %p - %m%n
# ***** R is set to be a RollingFileAppender.
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${catalina.home}/logs/demo.log
# ***** Max file size is set to 100KB
log4j.appender.R.MaxFileSize=100KB
# ***** Keep one backup file
log4j.appender.R.MaxBackupIndex=1
# ***** R uses PatternLayout.
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d [%c] %p - %m%n
Note:
The location of the log file is set in:
log4j.appender.R.File=${catalina.home}/logs/demo.log
- Create two jsp files in the C:\Tools\Tomcat 4.1\webapps\demo directory:
File 1: index.jsp
<%@ taglib uri="http://jakarta.apache.org/taglibs/log-1.0" prefix="log" %>
<html>
<head>
<title>Demonstration log4j usage in jsp</title>
</head>
<body>
<!-- Demonstrate different log levels -->
<log:debug>Show DEBUG message.</log:debug>
<log:info>Show INFO message.</log:info>
<log:warn>Show WARN message.</log:warn>
<log:error>Show ERROR message.</log:error>
<log:fatal>Show FATAL message.</log:fatal>
<!-- Demonstrate where to put the log messages -->
<log:fatal>Message embedded within the open and close tags.</log:fatal>
<log:fatal message="Message passed as an attribute to the tag" />
<log:fatal category="com.mobilefish.demo.index">
Using category attribute.
</log:fatal>
<b>
The log messages are shown in the Tomcat console and in the
${catalina.home}/logs/demo.log file.
</b>
</body>
</html>
File 2: test.jsp
<%@ page import="org.apache.log4j.Logger" %>
<html>
<head>
<title>Demonstration log4j usage in jsp</title>
</head>
<body>
<%
Logger log = Logger.getLogger("com.mobilefish.demo.test");
log.debug("Show DEBUG message");
log.info("Show INFO message");
log.warn("Show WARN message");
log.error("Show ERROR message");
log.fatal("Show FATAL message");
%>
<b>
The log messages are shown in the Tomcat console and in the
${catalina.home}/logs/demo.log file.
</b>
</body>
</html>
- Create the following directories inside the Tomcat "webapps" directory:
- (Re)start Tomcat.
- Access the index.jsp file:
http://localhost:8080/demo/index.jsp
- The following log messages are shown in the Tomcat console and in the ${catalina.home}/logs/demo.log file.
2006-06-03 17:28:43,379 [root] DEBUG - Show DEBUG message.
2006-06-03 17:28:43,409 [root] INFO - Show INFO message.
2006-06-03 17:28:43,409 [root] WARN - Show WARN message.
2006-06-03 17:28:43,409 [root] ERROR - Show ERROR message.
2006-06-03 17:28:43,419 [root] FATAL - Show FATAL message.
2006-06-03 17:28:43,419 [root] FATAL - Message embedded within the open and close tags.
2006-06-03 17:28:43,419 [root] FATAL - Message passed as an attribute to the tag
2006-06-03 17:28:43,419 [com.mobilefish.demo.index] FATAL - Using category attribute.
- Access the test.jsp file:
http://localhost:8080/demo/test.jsp
- The following log messages are shown in the Tomcat console and in the ${catalina.home}/logs/demo.log file.
2006-06-03 17:55:43,379 [com.mobilefish.com.test] DEBUG - Show DEBUG message.
2006-06-03 17:55:43,409 [com.mobilefish.com.test] INFO - Show INFO message.
2006-06-03 17:55:43,409 [com.mobilefish.com.test] WARN - Show WARN message.
2006-06-03 17:55:43,409 [com.mobilefish.com.test] ERROR - Show ERROR message.
2006-06-03 17:55:43,419 [com.mobilefish.com.test] FATAL - Show FATAL message.
- Change the root logger level to WARN in the log4j.properties file and restart Tomcat:
log4j.rootLogger=warn, stdout, R
When the index.jsp page is reloaded in your browser only the following messages are shown:
2006-06-03 17:48:43,409 [root] WARN - Show WARN message.
2006-06-03 17:48:43,409 [root] ERROR - Show ERROR message.
2006-06-03 17:48:43,419 [root] FATAL - Show FATAL message.
2006-06-03 17:48:43,419 [root] FATAL - Message embedded within the open and close tags.
2006-06-03 17:48:43,419 [root] FATAL - Message passed as an attribute to the tag
2006-06-03 17:48:43,419 [com.mobilefish.demo.index] FATAL - Using category attribute.