Using SL4J and SimpleLogger with Servlets

Page content

We saw in my previous blog that SLF4J and SimpleLogger support the most critical logging requirements with ease. In this blog, we will see how to use SLF4J in a Servlets based application.

The general practice is to initialize logging related objects and parameters at the startup of an application. In case of Servlets, any of the servlet in the Web Application can be invoked due to user action. So we need to use a different technique for initializing logging related objects.

Step -1

The first step is to implement the ServletContextListener. Here is a Java class that implements the ServletContextListener interface.

import org.slf4j.Logger;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.*;

import static org.slf4j.LoggerFactory.getLogger;

 * Created with IntelliJ IDEA.
 * User: sriram

public class ListenerForAllServletEvents implements ServletContextListener {

    Logger logger = null;

    public void contextInitialized(ServletContextEvent servletContextEvent) {
        System.setProperty("org.slf4j.simpleLogger.dateTimeFormat", "yyyy-MM-dd::HH-mm-ss-SSS");
        logger = getLogger(this.getClass().getName());
        logger.error("I have hit contextInitialized: "+servletContextEvent.getServletContext().toString());

    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        logger. debug("I have hit contextDestroyed.");


Step -2

The next step is to add an annotation called @WebListener. This is already shown in the above code snippet.

Now when you package and deploy your Servlets based Web Application, you will see that SLF4J Logger (SimpleLogger) has been appropriately initialized. And you should be able to use the SLF4J Logger classes in the rest of your Web Application code.

Here is the analysis of what happened behind the screen as a result of these two steps

  1. With Servlets 3.0 specification we can use annotation such as @WebListener to annotate a class to be a Servlets event listener. The container will examine the event listeners interfaces implemented by the class to decide how to associate the listener class.
  2. Any class that implements ServletContextListener receives events when the whole Web Application is deployed and initialized.
  3. When the application is deployed, the container (Tomcat for example) will create an instance of the listener class and associate it with the ServletContext.
  4. When you invoke getLogger of LoggerFactory for the first time, internally an instance of SimpleLogger class is created
  5. SimpleLogger uses private static variables to store different log parameters such as Log Level, date format etc.
  6. These log parameters are initialized only when the first instance of class _SimpleLogger _is created.
  7. In our example this initialization of log parameters happens as part of the contextIntialized method which is invoked at the start of our Web Application.
  8. When other application Servlets are loaded and executed, the subsequent calls to getLogger result in the reuse of the various log parameters that were initialized earlier (static variables).