Message and trace logging

 

+

Search Tips   |   Advanced Search

 

Portlets can write message and trace information to log files, which are maintained in the portal_server_root/log/ directory. The log files help the portal administrator investigate portlet errors and special conditions and help the portlet developer test and debug portlets.

The PortletContext object in the standard portlet API includes a log() method. The Hello User example shows how to log a message when exceptions are caught.

public void processAction(ActionRequest request, 
                          ActionResponse response)
                          throws PortletException, IOException {

  PortletContext context = getPortletContext();

  try   {
    String save = request.getParameter("save");
    if (save != null)
    {
      PortletPreferences prefs = request.getPreferences();
      prefs.setValue("userName",request.getParameter("username"));
      prefs.store();
    }
  }
  catch ( IOException ioe )
  {
    context.log("An IO error occurred when trying to save the name.");    
  }
  catch ( PortletException pe ) 
  {
    context.log("A portlet exception was thrown when trying to save the name.");    
  }

}

The IBM Portlet API provides the PortletLog class, which has methods to write specific types of message and trace information to the logs.

If an IBM portlet needs to access the portlet log multiple times in a method, it is good idea to assign the log reference to a variable, for example:

  private PortletLog myLogRef = getPortletLog();

Since logging operations are expensive, the PortletLog class of the IBM Portlet API provides methods to determine if logging is enabled for a given level. The portlet should write to the log of a given level only if the log is tracking messages of that level. For example:

  
  if( getPortletLog().isDebugEnabled() )
  {
    myLogRef.debug("Warning, Portlet Resources are low!");
  }

 

Enable logging for portlets

Portlet logging is enabled by setting the following trace strings in the log.properties file.

Standard portlets

javax.portlet.Portlet=all

The Java Portlet Specification defines that log statements from the JSR portlets are also passed on to the ServletContext.log() method, which will additionally write them to System.out in the default configuration.

IBM portlets

org.apache.jetspeed.portlet.PortletLog=all

See WebSphere Portal run-time logs for a description of how trace strings are set in the log.properties file.

 

Related information