14.2.5 Adding logging support
The Struts Portlet Framework uses the Commons-Logging interface for a logging facility. The Struts Portlet Framework has supplied an implementation of the Commons-Logging Log interface that can be used to map the trace messages to the logging facility used by the portal server. This file is normally found in the wp_root/log directory. Trace logging is enabled by setting properties in the wp_root/shared/app/config/log.properties file. By default, tracing is disabled. The trace string for tracing a Struts portlet application can be configured in log.properties. The trace string can be modified to add or remove classes.
To start logging our application, proceed as follows:
1.
| First, enable tracing of struts portlets; proceed as follows:
|
a.
Open the wp_root/shared/app/config/log.properties, where wp_root is the folder where the Portal test environment was installed.
|
b.
Go to the end of the file and add the following traceString.
| traceString=org.apache.struts.*=all=enabled: com.ibm.wps.portlets.struts.*=all=enabled: com.ibm.wps.struts.common.*=all=enabled: com.ibm.wps.struts.base.*=all=enabled: com.ibm.wps.portlets.struts.logging.WpsStrutsTraceLogger=all=enabled: myfirststruts.*=all=enabled
Important: You can have only one traceString in the log.properties file, and the entire traceString should be specified in only one line.
|
c.
Save the file and close it.
|
2.
| Additionally, the Struts Portlet Framework specifies the common logging Log Factory in the META-INF/services directory. The log factory class name is dependent on the WebSphere Portal container. To specify the correct commons LogFactory for our application, proceed as follows:
|
a.
Expand Dynamic Web Projects | MyFirstStruts | WebContent.
|
b.
Right-click the META-INF folder. Select New | Folder.
|
c.
Enter services as the folder name.
|
e.
Right-click the services folder. Select New | Other.
|
f.
In the Select a wizard window, expand Simple. Select File.
|
h.
Enter org.apache.commons.logging.LogFactory as the file name.
|
j.
Paste the following line in the file:
| com.ibm.wps.portlets.struts.logging.StrutsLogFactory
k.
Save the file and close it.
|
3.
| Now let's add some code in the welcome action mapping.
|
a.
Edit myfirststruts.actions.WelcomeAction.
|
b.
Add the following instance variable:
| private Log log = LogFactory.getLog(this.getClass());
c.
Right-click anywhere in the code and select Source | Organize Imports.
|
d.
Select org.apache.comons.logging.Log for the Log class and click Next >.
|
e.
Select org.apache.commons.logging.LogFactory for the LogFactory class and click Finish.
|
f.
Add the following code at the beginning of the execute method:
| if (log.isTraceEnabled()) { log.trace("###Welcome###"); }
g.
Finally, add the following code in the catch statement:
| if (log.isDebugEnabled()) { log.debug("WelcomeAction: Error determining if user is configured"); }
h.
Save the file and close the editor.
|
4.
| Now you can run the application again, open the log file at wp_root/log folder and look for the Welcome message. You should see a message similar to the following:
| 2005.02.01 17:25:10.461 l myfirststruts.actions.WelcomeAction trace
Servlet.Engine.Transports : 1
###Welcome###
Note 1: You have to restart the server if it is still running, since you made changes to the configuration files.
|
|