Logging and tracing
All WebSphere Commerce assets can use the LoggingHelper utility class provided in the com.ibm.commerce.foundation.common.logging package for tracing and logging. This utility helps to resolve the Java Logger (java.util.logging.Logger) associated with the package of the calling class. Once the Logger is resolved, the library used to log messages is java.util.logging.Logger.The code sample below shows how to use the logger object for tracing. For more information see the Adding logging and tracing to the application topic in the WAS information center.
Adding trace to the code typically results in creating new objects. You should check whether logging is enabled for your class by calling the LoggingHelper.isTraceEnabled(logger) method before calling the logging methods.
Logger logger = LoggingHelper.getLogger(ExceptionData.class); if (LoggingHelper.isTraceEnabled(logger)) { StringBuffer sb = new StringBuffer(80); sb.append("Could not resolve message key '"); sb.append(_strMessagekey); sb.append("' from the properties file "); sb.append(logger.getResourceBundleName()); logger.logp(LoggingHelper.DEFAULT_TRACE_LOG_LEVEL, CLASSNAME, METHODNAME, sb.toString()); }Methods which are not automatically generated should have trace entry and exit points. All trace entry statements must be matched with exit statements. Trace entry statements typically include input parameters if there are any, while exit statements include the return value(s). The entry and exit trace statements should be put inside the if-then block to check if your component trace is enabled. Below is a sample code for tracing method's entry and exit points:
public class GetOrderExpressionBuilder { private static final String CLASSNAME = GetOrderExpressionBuilder.class.getName(); private static final Logger LOGGER = LoggingHelper.getLogger(GetOrderExpressionBuilder.class); /** * Forms an expression to query orders by status. */ public ExpressionType findByOrderStatus(Map amNVPs) { final String METHODNAME = "findByOrderStatus"; if (LoggingHelper.isEntryExitTraceEnabled(LOGGER)) { LOGGER.entering(CLASSNAME, METHODNAME, amNVPs); } ExpressionType findByOrderStatusExpression = null; // business logic... if (LoggingHelper.isEntryExitTraceEnabled(LOGGER)) { LOGGER.exiting(CLASSNAME, METHODNAME, findByOrderStatusExpression); } return findByOrderStatusExpression; } }Related concepts
Command error handling
Execution flow tracingRelated tasks
Create messages