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

Your portlet application must contain the log4j.jar.

A valid log4j configuration must be provided.

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="http://jakarta.apache.org/log4j/">

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

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

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

<appender name="Console"

class="org.apache.log4j.ConsoleAppender">

<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"/>

</layout>

</appender>

<root>

<priority value="INFO"/>

<appender-ref ref="Console"/>

</root>

</log4j:configuration>

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;

import java.net.URL;

public class ConfigureLog4jListener

implements ServletContextListener

{

@Override

public void contextInitialized(

ServletContextEvent aServletContextEvent)

{

URL log4jConf = this.getClass()

.getClassLoader()

.getResource("../log4j.xml");

DOMConfigurator.configure(log4jConf.getFile());

}

@Override

public void contextDestroyed(

ServletContextEvent aServletContextEvent)

{

}

}

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