| By Fred Simmons, Jeng Yoong Tan | Article Rating: |
|
| May 26, 2005 10:15 AM EDT | Reads: |
28,416 |
Adding logging to portlet code is usually an afterthought during the development cycle. The issue of logging usually only comes up after a portlet has been deployed in production and an error occurs. Then you hear, "Well, the problem didn't happen during testing."
This paper steps you through the three different logging mechanisms available for portlets. The first demonstrates the WebSphere Portal's built-in logging mechanism. The second is a roll-your-own version of the base WebSphere Portal logging. The third shows you how to add Log4j to your portlet.
Add WebSphere PortletLog to a Basic Portlet
Start by creating a basic portlet using the IBM Rational Software Development Platform. All of the examples in this article should be run in the J2EE perspective. Using the navigation bar, go to File=>New=>Other=>Portlet Project. Click Next. Enter the project name Test Log. Press the Next button. Select Basic Portlet. Press the Finish button.
Using the native WebSphere PortletLog logging for your portlet development has a few drawbacks. So you understand both its strengths and weaknesses, you can try this logging feature with the Test Log project.
Open TestLogPortlet.java and add the declaration for the PortletLog and sClassName. Replace the init() [A], doView() [B] and actionPerformed() [C] methods with the code shown in Listing 1 at the end of this article. Remove the annotations [...] from the listing before you compile.
Besides instantiating a reference to the PortletLog [D] in init(), the logging methods info() [E] and error() [F] are added to both doView() and actionPerformed(). To prevent a negative impact on performance always check to see if logging is enabled using
if (logger.isInfoEnabled()) ...
before invoking info(). Also limit the number of info() calls to significant events such as method entry and exit to prevent the portlet from spending more time tracing than actually performing its function. Since error() is only invoked under extraordinary circumstances, you don't need to check if error logging is enabled before invoking this method.
Logging in the WebSphere Portal Server consists of three distinct levels and one all-encompassing level (all):
- low
- medium
- high
- all
Although PortletLog supports different logging levels, the best rule for the PortletLog tracing is to keep the assignments simple. Limit your level assignments to all=enabled or all=disabled.
Enabling PortletLog Tracing Using the Rational Software Development Platform
Test the Test Log portlet in IBM Rational Software Development Platform. Right click on Test Log and choose Run=>Run on Server. Choose Manually define a server and WebSphere Portal v5.0 Test Environment. Select the Set server as project default checkbox. Press Next.
Notice the port number. The portal login page for this test environment will have the address http://localhost:9081/wps/myportal. Click Next.
Add Test LogEAR to the list of Configured projects.
The WebSphere Portal Server test environment will display the portlet. In the Servers view, right click on the WebSphere Portal v5.0 Test Environment. Press Stop. After the server stops, right click on the server and select Open. Choose the Portal Options tab. Select the Enable base portlets for portal administration and customization and Enable console logging checkboxes. Press Ctrl-S to save your configuration changes and close the server configuration panel.
Right click on the Test Log portlet project again and choose Run=>Run on server.
Due to the design of PortletLog tracing, you need the administration console to enable portlet logging. Once the portlet renders, navigate to Administration=>Portal Analysis=>Enable Tracing. You enable tracing by adding the line org.apache.jetspeed.portlet.PortletLog=all=enabled to the Append these trace settings field. Press the plus (+) button. Log out of the portal and then logon. After the page with the Test Log portlet displays, press the Submit button and select the Console tab. You should see messages similar to those shown in Figure 5.
Results of Using PortletLog Logging
Notice that in Figure 5 all log entries generated by the PortletLog are prefaced with org.apache.jetspeed.portlet.PortletLog. It's important that any log messages include a unique string. In the example above, the logging text for both the info() [E] and error() [F] methods include the sClassName variable in addition to the method name. Also, if all your portlets use the PortletLog methods, then logging for all the portlets in your portal will be enabled. This can be a nuisance if you're just trying to solve a problem in a single portlet.
The benefit of using the PortletLog is that it's simple. It's included in the base portlet class. You can get a reference to the PortletLog using:
- portletConfig.getContext().getLog(), or
- getPortletLog(), which is available in the PortletAdapter.
Published May 26, 2005 Reads 28,416
Copyright © 2005 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
![]() |
cshb 07/30/07 09:09:14 AM EDT | |||
http://websphere.sys-con.com/read/85890_3.htm "Paste the text in Listing 5 at the end of this article into the log4j.properties file and modify it to suit your environment. Remove the annotations, [...]." I have registered to this page, but still do not see the listings!!! |
||||
![]() |
sanjeev 01/26/06 05:24:40 AM EST | |||
The article is informative. Sanjeev. |
||||
![]() |
Hans-Henrik Basse 11/09/05 08:51:35 AM EST | |||
The arcticle seems very good and usefull. But both the listning and the was file is missing could you mail it to me. |
||||
- Oracle To Keynote Cloud Computing Expo
- Is the PR Business Extinct? Yes
- The Difference Between Web Hosting and Cloud Computing
- Government IT & Cloud Computing: Themes for Discussion
- GovIT Expo Highlights Cloud Computing
- The End of IT 1.0 As We Know It Has Begun
- Cloud Computing Best Practices
- Gang of Four Creates Cloud BI Stack
- The Case for Single-Purpose Services
- VIP Invitation For the GovIT Panel October 6, Washington DC
- Oracle To Keynote Cloud Computing Expo
- How to Diagnose Java Resource Starvation
- Is the PR Business Extinct? Yes
- Anatomy of a Java Finalizer
- IBM & Cloud Computing: Exclusive Q&A
- IBM & Cloud Computing: How "SOA in the Cloud" Can Produce Real Change
- SOA & Cloud Bootcamp: Comparing Cloud Computing Providers
- WebSphere Guru to Keynote at SOA World
- IBM Researcher Solves In-Cloud Data Encryption Puzzle
- The Difference Between Web Hosting and Cloud Computing
- Java vs C++ "Shootout" Revisited
- Where Are RIA Technologies Headed in 2008?
- WebSphere Application Server Java Dumps
- Breaking News: New Internal IBM Report Says "Another Flawed Study"
- Last Exclusive JDJ Interview With "IBM's" John A. Swainson, Now CA's Newly Appointed CEO
- How To Deploy Scalable WebSphere Applications Using "Maven" Build Tool
- Your Guide to Portal Clustering in WebSphere Portal Server 5.1
- Developing Java and Web Services Applications on Rational Application Developer V6
- Automated Deployment of Enterprise Application Updates
- Putting IBM's WAS On Unix - WebSphere Application Server



































