SiteVision currently uses Log4j. Any logging framework can be used by a portlet application.
There are some considerations when using log4j since a portlet application is using another class loader than the ROOT application (i.e. SiteVision).
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:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>My portlet app</display-name>
<description>My portlet app</description>
<listener>
<listener-class>
org.mystuff.ConfigureLog4jListener
</listener-class>
</listener>
</web-app>
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.*;
import java.io.IOException;
import java.io.PrintWriter;
public class MyPortlet
extends GenericPortlet
{
private Logger log4jLogger;
@Override
public void init() throws PortletException
{
super.init();
log4jLogger = Logger.getLogger(getClass().getSimpleName());
}
@Override
protected void doView(RenderRequest request,
RenderResponse response)
throws PortletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
...
log4jLogger.info("Some log message...");
...
}
}
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: