Using SL4J and SimpleLogger with Servlets

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.

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).
  • SeaGreen Splash

    Thanks alot for this article, it helped me alot to understand how Servlet contextListener works. I am new in using Servlets, I have a query, it will be great if you could provide your view on it – I have one Servlet class and 3/4 java classes which have many methods. The servlet class creates instance of another java class which calls other class method and so on. I need to log all the activity in all classes. But currently only servlet class i can log that to with system.out.println in server log. i want to use slf4j but how to include that in all classes could you suggest.

    • Hi SeaGreen

      Not sure if I understood the problem fully but there are many ways to log from the “non-Servlet” class. One simple way is to pass the “SLF4J” logger object as parameter to the Classes you are instantiating. That way each class will have the same logger instance. You can then invoke the logger functions inside the other classes.

      You can also create individual SLF4J logger instances in each of these classes and use them.