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="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...");

...

   }

}

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!

Denna sida publicerades: 2016-09-30

Skriv ut PDF-avsnitt

Hjälpte informationen på den här sidan dig?

Missa inga nya rön, tips och tricks! Prenumerera på insite!

sitevision.eu

Find us!

SiteVision AB (Headquarter)
Vasagatan 10
702 10 Orebro
Sweden

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

Latest Tweets