Help SiteVision 4

What logging framework should I use?

SiteVision currently uses Log4j. Any logging framework can be used by a portlet application.

Using log4j as logging framework

There are some considerations when using log4j since a portlet application is using another class loader than the ROOT application (i.e. SiteVision).

  1. Your portlet application must contain the log4j.jar.
  2. A valid log4j configuration must be provided.
  3. The log4j framework must be aware of the configuration.

Here is a log4j.xml example configuration that logs to the SiteVision wrapper log by using a "Console appender" (i.e. uses the System.out writer).

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="">

   <!-- ============================== -->

   <!-- Append messages to the console -->

   <!-- ============================== -->

   <appender name="Console"


      <param name="Target" value="System.out"/>

      <param name="Threshold" value="DEBUG"/>

      <layout class="org.apache.log4j.PatternLayout">

         <param name="ConversionPattern"

value="%d{HH:mm:ss.SSS} %-5p [%c{1}] [%t] [%x] %m%n"/>




      <priority value="INFO"/>

      <appender-ref ref="Console"/>



To make log4j aware of the configuration, you would typically use a ServletContextListener to load the configuration whenever context is created (i.e. portlets are deployed/re-deployed). Below is an example (where the log4j.xml file is located in the WEB-INF directory of the application):

package org.mystuff;

import org.apache.log4j.xml.DOMConfigurator;

import javax.servlet.ServletContextEvent;

import javax.servlet.ServletContextListener;


public class ConfigureLog4jListener

implements ServletContextListener



   public void contextInitialized(

ServletContextEvent aServletContextEvent)


      URL log4jConf = this.getClass()






   public void contextDestroyed(

ServletContextEvent aServletContextEvent)




Your Listener must of course be registered in the web.xml of your portlet application. An example:

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "">


   <display-name>My portlet app</display-name>

   <description>My portlet app</description>







When using a GenericPortlet-based portlet, you would typically initialize the Logger instance via the init method.

package org.mystuff;

import org.apache.log4j.Logger;

import javax.portlet.*;



public class MyPortlet

        extends GenericPortlet


   private Logger log4jLogger;


   public void init() throws PortletException



      log4jLogger = Logger.getLogger(getClass().getSimpleName());



   protected void doView(RenderRequest request,

RenderResponse response)

throws PortletException, IOException



      PrintWriter out = response.getWriter();

..."Some log message...");




Known issues/caveats

Using log4j without a specified configuration will typically produce the following output in the SiteVision wrapperlog once.

log4j:WARN No appenders could be found for logger (MyPortlet).

log4j:WARN Please initialize the log4j system properly.

log4j:WARN See ...

Note that log4j will print the above output once only! Subsequest calls to a Logger will be completely silent!

The page published: 2016-09-30

Find us!

SiteVision AB (Headquarter)
Vasagatan 10
702 10 Orebro

Info: +46 19-17 30 30
Support: +46 19-17 30 39