Search This Blog

Friday, December 18, 2009

Logger in JSP

Apply log4j within jsp.


Information
none

Operating system used
Windows XP Home Edition Version 5.1 SP 2

Software prerequisites
Tomcat
Jakarta log tag library
Log4j 1.2.9


Procedure

1. Download the Jakarta log tag library: jakarta-taglibs-log-1.0.zip

2. 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

3. Download Log4j 1.2.9: logging-log4j-1.2.9.zip

4. 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

5. Install Tomcat. Follow guide "Installing Tomcat 4.1.31".

6. 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:


PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">


Demonstration log4j usage in jsp

index.jsp


http://jakarta.apache.org/taglibs/log-1.0
/WEB-INF/taglibs-log.tld



Note: You MUST add a element in the web.xml file:


http://jakarta.apache.org/taglibs/log-1.0
/WEB-INF/log.tld


* 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" %>



Demonstration log4j usage in jsp





Show DEBUG message.
Show INFO message.
Show WARN message.
Show ERROR message.
Show FATAL message.


Message embedded within the open and close tags.


Using category attribute.



The log messages are shown in the Tomcat console and in the
${catalina.home}/logs/demo.log file.





File 2: test.jsp

<%@ page import="org.apache.log4j.Logger" %>



Demonstration log4j usage in jsp




<%
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");
%>


The log messages are shown in the Tomcat console and in the
${catalina.home}/logs/demo.log file.





7. (Re)start Tomcat.

8. Access the index.jsp file:

http://localhost:8080/demo/index.jsp

9. 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.

10. Access the test.jsp file:

http://localhost:8080/demo/test.jsp

11. 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.

12. 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.

No comments:

Hit Counter


View My Stats