6.7 How to use log4j

 < Day Day Up > 



6.7 How to use log4j

Here is a simple example of how log4j can be used with the portlet.

Before working through the example, use the following steps to obtain log4j functionalities:

  1. Download the log4j distribution from:

    http://jakarta.apache.org/log4j/docs/download.html

  2. Extract the archived files to some suitable directory.

  3. Import the file dist/lib/log4j-1.2.6.jar to your development environment. Select File Import File System, browse to the log4j jar file, and import it to the portlet WEB-INF/lib folder.

Example 6-12 is a very simple example of a program implementing a SimpleLayout and FileAppender. It will write a log to the outputfile.txt file.

Example 6-12: Adding log4j to HelloWorldFromDomino portlet

start example
 ///Import import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.log4j.SimpleLayout; import org.apache.log4j.FileAppender; public class HelloWorldFromDomino extends AbstractPortlet {    static Logger logger = Logger.getLogger(HelloWorldFromDomino.class);    public void init(PortletConfig portletConfig) throws UnavailableException {      SimpleLayout layout = new SimpleLayout();       FileAppender appender = null;       try {          appender = new FileAppender(layout,"outputfile.txt",false);       } catch(Exception e) {}       logger.addAppender(appender);       logger.setLevel((Level) Level.DEBUG);       logger.debug("Here is some DEBUG");       logger.info("Here is some INFO");       logger.warn("Here is some WARN");       logger.error("Here is some ERROR");       logger.fatal("Here is some FATAL");       // Initializing       super.init(portletConfig);    }    public void doView(PortletRequest request, PortletResponse response) throws PortletException,IOException{       // Calling method getSession in viewmode       logger.debug("We are now in doView!");        getSession(request,response);    }    private Session getSession(PortletRequest request, PortletResponse response)throws PortletException,IOException {       // Check first if session already exist       logger.debug("Trying to create diiopsession!");       Session diiopsession = (Session)request.getSession().getAttribute("diiopsession"); ... 
end example

A more sophisticated way to do this is to put all logging settings in the ResourceBundle property file, and also implement class name ahead of every log. That way you are able to configure the settings in an external file (switch logging off and on, set log level, and so forth etc.) and get more informative and useful logs.

To do this, perform the following steps:

  1. Create a properties file for log4j. Select File New Simple File; name the log4j.properties. This is illustrated in Figure 6-15.

    click to expand
    Figure 6-15: Creating log4j.properties file

  2. Open your log4j.properties file and copy the following code into the newly created file.

    Example 6-13: Configuration file for logging

    start example
     # Initialise root logger level DEBUG and call it A1. log4j.rootLogger=DEBUG, A1 # A1 is set to be a FileAppender. log4j.appender.A1=org.apache.log4j.FileAppender # A1 uses PatternLayout. log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n log4j.appender.A1.File=C:/OurOwnPortletLogFile.txt log4j.appender.A1.append=true 
    end example

  3. Save the configuration file. Logging will be written to the c:/OurOwnPortletLogFile.txt file. More possible configuration settings are discussed on the Apache Web site's log4j documents page.

  4. Import the logging.jar file to your portlet. Logging.jar is a simple logging utility with support for logging filters.

    The structure of your project should now resemble the one shown in Figure 6-16.


    Figure 6-16: Structure of portlet in second logging sample

  5. You can now start to put your log calls anywhere in the portlet code you want to. Example 5-25 illustrates this.

    Example 6-14: Another example of sophisticated portlet logging

    start example
     .. //Import import com.ibm.logging.Log; public class HelloWorldFromDomino extends AbstractPortlet {    public void init(PortletConfig portletConfig) throws UnavailableException {       // Initializing       Log.debug(getClass(), "Initializing portlet");       super.init(portletConfig);    }    public void doView(PortletRequest request, PortletResponse response) throws PortletException,IOException{       // Calling method getSessios in viewmode       Log.debug(getClass(), "Trying to get session..");        getSession(request,response);        Log.debug(getClass(), "Get session method ran..");    }    private Session getSession(PortletRequest request, PortletResponse response)throws PortletException,IOException {       // Check first if session already exist       Log.debug(getClass(), "Trying to get session from session");       Session diiopsession = (Session)request.getSession().getAttribute("diiopsession"); 
    end example



 < Day Day Up > 



Portalizing Domino Applications for Websphere Portal
Portalizing Domino Applications for Websphere Portal
ISBN: 0738499811
EAN: 2147483647
Year: 2003
Pages: 103
Authors: IBM Redbooks

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net