Logging custom business event using Site Analysis

WebSphere Portal Server provides com.ibm.portal.portlet.service.siteanalyzer.PortletSiteAnalyzerLoggingServiceHome that we can use for logging custom business events. When you write a log message using this service it goes to the same log file as that of other server side analysis log file and it is written using same log format as that of the Server side analysis logs

Important Note By default the WPS server only writes log messages in the render phase. So if you want one of the following features then you will have to use the PortletSiteAnalyzerLoggingServiceHome for logging messages

  • You want to log a custom business event

  • You want to log action or resource or event phase event

  • In the render phase you want to include the render parameter in the log information


In general if you use the PortletSiteAnalyzerLoggingServiceHome, you will get more flexibility

I built a sample portlet to try the service and you can download it from here


package com.webspherenotes.wps70.sa;

import java.io.IOException;

import javax.naming.NamingException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.portlet.service.siteanalyzer.ParameterNamesProcessor;
import com.ibm.portal.portlet.service.siteanalyzer.PortletSiteAnalyzerLogger;
import com.ibm.portal.portlet.service.siteanalyzer.PortletSiteAnalyzerLoggingServiceHome;

public class SiteAnalyticsDebugPortlet extends GenericPortlet {

PortletSiteAnalyzerLoggingServiceHome portletSiteAnalyzerLoggingServiceHome;
public void init() throws PortletException {
System.out.println("Entering SiteAnalyticsDebugPortlet.init()");
com.ibm.portal.portlet.service.PortletServiceHome psh;
try {
javax.naming.Context ctx = new javax.naming.InitialContext();
psh = (PortletServiceHome) ctx
.lookup(PortletSiteAnalyzerLoggingServiceHome.JNDI_NAME);
portletSiteAnalyzerLoggingServiceHome = (PortletSiteAnalyzerLoggingServiceHome) psh
.getPortletService(PortletSiteAnalyzerLoggingServiceHome.class);
} catch (javax.naming.NameNotFoundException e) {
e.printStackTrace(System.out);
} catch (NamingException e) {
e.printStackTrace(System.out);
}

System.out.println("Exiting SiteAnalyticsDebugPortlet.init()");
}

protected void doEdit(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
response.setContentType("text/html");
getPortletContext().getRequestDispatcher("/mode.jsp").include(request,
response);
}

protected void doHelp(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
response.setContentType("text/html");
getPortletContext().getRequestDispatcher("/mode.jsp").include(request,
response);
}

protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
response.setContentType("text/html");
PortletSiteAnalyzerLogger logger =
portletSiteAnalyzerLoggingServiceHome.getLogger(request, response);
if (logger.isLogging()) {
logger.log("SampleRenderEvent");
}

getPortletContext().getRequestDispatcher("/view.jsp").include(request,
response);
}

public void processAction(ActionRequest request, ActionResponse response)
throws PortletException, IOException {
System.out.println("Inside SiteAnalyticsDebugPortlet.processAction");
PortletSiteAnalyzerLogger logger = portletSiteAnalyzerLoggingServiceHome
.getLogger(request, response);
if (logger.isLogging()) {
logger.log("SampleActionEvent");
}

response.setRenderParameter("com.webspherenotes.test", "testValue");
}

public void serveResource(ResourceRequest request, ResourceResponse response)
throws PortletException, IOException {
response.setContentType("text/html");
response.getWriter().println(
"Hello from SiteAnalyticsDebugPortlet.serveResource()");
PortletSiteAnalyzerLogger logger = portletSiteAnalyzerLoggingServiceHome
.getLogger(request, response);
if (logger.isLogging()) {
logger.log("Sample serveResource event");
}

}
}


In the init() method of the portlet i am getting instance of PortletSiteAnalyzerLoggingServiceHome. Then whenever we want to log event we have to first get logger and then write message like this

This is how you log the message

public void processAction(ActionRequest request, ActionResponse response)
throws PortletException, IOException {
System.out.println("Inside SiteAnalyticsDebugPortlet.processAction");
PortletSiteAnalyzerLogger logger = portletSiteAnalyzerLoggingServiceHome
.getLogger(request, response);
if (logger.isLogging()) {
logger.log("SampleActionEvent");
}

response.setRenderParameter("com.webspherenotes.test", "testValue");
}

This is the output in the log file

localhost.localdomain - wasadmin [09/Sep/2010:06:52:07 -0700] "GET /Portlet/Z5_OGFLMKG108IGF0IANFDTU13085/Site_Analytics_portlet/Sample%20action%20event?PortletPID=Z5_OGFLMKG108IGF0IANFDTU13085&PortletMode=view&PortletState=normal&RequestType=action HTTP/1.1" 200 -1 "http://localhost/Page/Z6_OGFLMKG108IGF0IANFDTU130G6/Site_Analytics" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.9) Gecko/20100824 Firefox/3.6.9" "JSESSIONID=0000iLVC75NHzLRaJr-0mNVA6T5:-1"


This code in the doView() method

protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
response.setContentType("text/html");
PortletSiteAnalyzerLogger logger =
portletSiteAnalyzerLoggingServiceHome.getLogger(request, response);
if (logger.isLogging()) {
logger.log("SampleRenderEvent");
}

getPortletContext().getRequestDispatcher("/view.jsp").include(request,
response);
}


It generates following message. Did you notice that it includes the render parameter

localhost.localdomain - wasadmin [09/Sep/2010:06:56:41 -0700] "GET /Portlet/Z5_OGFLMKG108IGF0IANFDTU13085/Site_Analytics_portlet/SampleRenderEvent?PortletPID=Z5_OGFLMKG108IGF0IANFDTU13085&PortletMode=view&PortletState=normal&RequestType=render&com.webspherenotes.test=testValue HTTP/1.1" 200 -1 "http://localhost/Page/Z6_OGFLMKG108IGF0IANFDTU130G6/Site_Analytics" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.9) Gecko/20100824 Firefox/3.6.9" "JSESSIONID=0000iLVC75NHzLRaJr-0mNVA6T5:-1"

1 comment: