Recipe 14.8 Logging Messages Using a Session Event Listener


You want to log messages in a custom-designed manner from a session event listener.


Design a session event listener that uses a log4j logging mechanism.


The servlet container notifies a session event listener class when it creates a new HttpSession , as well as when it is about to invalidate or expire a session. Web applications use sessions to track a user 's progress through the web application, typically by identifying him with a cookie named JSESSIONID . See Chapter 10 for more information on cookies, and Chapter 11 for detailed coverage of sessions.

Example 14-14 implements the javax.servlet.http.HttpSessionListener interface and the interface's two methods : sessionCreated( ) and sessionDestroyed( ) . The code logs messages relating to new sessions in sessionCreated( ) and relating to invalidated sessions in sessionDestroyed( ) .

Example 14-14. Using log4j in a session event listener
 package com.jspservletcookbook;  import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator;  import javax.servlet.*; import javax.servlet.http.*; public class SessionLogger implements HttpSessionListener {   private Logger log;   public SessionLogger( ){       /*     The loggers are typically initialized by a special initialization      listener or servlet.  If this is not the case, then initialize the     logger here:     java.util.ResourceBundle bundle =       java.util.ResourceBundle.getBundle(       "");     PropertyConfigurator.configure(bundle.getString(       "log-configure-path"));    */  log = Logger.getLogger(SessionLogger.class);  }  public void sessionCreated(HttpSessionEvent se)   {          //log request of the INFO level"HttpSession created: " + se.getSession( ).getId( ));        }         public void sessionDestroyed(HttpSessionEvent se) {          //log request about sessions that are invalidated"HttpSession invalidated: " + se.getSession( ).getId( ));          }  } 

Give this class a no-args constructor, place it in WEB-INF/classes or in a JAR located in WEB-INF/lib , and register it in web.xml :

 <listener>     <listener-class>     com.jspservletcookbook.SessionLogger     </listener-class> </listener> 

The SessionLogger class gets a logger in its constructor; it depends on the application already having initialized the log4j logging mechanism in a servlet or in the servlet context listener (as in Recipe 14.4).

A web application can configure its log4j mechanism using a special initialization servlet or listener, so the other classes or beans that do logging do not have to handle the log4j configuration stage. You can initialize the log4j logging mechanism using a servlet such as the one shown in Recipe 14.6.

The commented-out code in the constructor shows another way that this listener class could configure its own logger in the event that the application has not yet configured the logging mechanism.

Here is the message logged to the Tomcat console after the first request to a servlet or JSP that participates in session tracking:

 INFO - HttpSession created: A65481C53B92F869BD18961D635BBF52 

When the session is invalidated, the console text is:

 INFO - HttpSession invalidated: A65481C53B92F869BD18961D635BBF52 

Like the listener described in Recipe 14.7, the session listener's logger inherits the logging destinations or appenders from the com.jspservletcookbook logger. The configuration file of Example 14-13 shows how this logger is set up to send messages to both the console and an example.log file. The log file's appender layout is specified using a PatternLayout , which is a different layout than the one used with the console appender. Here is example text from this log when the servlet container invalidates a session:

 INFO  Logger:SessionLogger Date: 2003-05-12 20:41:05,367 - HttpSession invalidated:  A65481C53B92F869BD18961D635BBF52 

See Also

Recipe 14.2 on downloading and setting up log4j ; Recipe 14.3 on using a log4j logger without a properties file; Recipe 14.4 on adding an appender to the root logger; Recipe 14.5 on using a pattern layout with a logger's appender; Recipe 14.6 on using a logger with a JSP; Recipe 14.8 on using log4j with session event listeners; the log4j download site:; the log4j Javadoc page:; the log4j project documentation page:

Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: