Use Extensions to Log Errors

Create an order management extension that logs messages to the same applications log file and uses the same application settings that an Oracle Application uses.

Avoid degrading performance during logging.

  • Use the SEVERE logging level only for extremely important conditions, such as a read error condition. Logging all events as SEVERE will produce a large number of log messages and degrade performance.

  • Use the FINEST logging level to get detailed reporting. Use FINEST for entry and exit logging. The Logger Method can write logs at different levels.

  • Avoid concatenating strings. If your log statement must concatenate strings, then write your code so it makes sure logging for the targeted logging level is enabled before you concatenate.

    Use the Logger method to avoid code readability problems that sometimes happen when you write code that makes sure the logging level is enabled, and when you include this code for every log statement. The Logger Method provides an alternative way to log for each level. It can use a format string and parameters as input. It can substitute the parameters in the format string only if the logging is enabled at the targeted level before writing to the log. This approach postpones string manipulation until your code confirms that logging is enabled.

Learn how to set log levels. For details, see java.util.logging.Level in Java Platform, Standard Edition API Specification.

This topic uses example values. You might need different values, depending on your business requirements.

Write error messages to logs when using order management extensions:

  1. Write to the log.

    Code

    Description

    def logger = context.getLogger();

    Get the logger object from the context. The first few lines have been left out of the example.

    def item = getItem(inventoryItemId, orgId);

    Get the item. Use item Id and organization Id when calling the getItem method.

    String hazardous = item.getAttribute("HazardousMaterialFlag");

    Get the HazardousMaterialFlag attribute from the item.

    if( "Y".equals(hazardous) ) {

    Determine whether the item is hazardous.

    logger.logFinest("Found line with hazardous item %s, %s", inventoryItemId, orgId);

    Log at the finest level. Note that the string format provides the first argument and the subsequent arguments provide the parameters.

    def packShipInstruction = line. getOrCreateContextRow("PackShipInstruction");

    Get the row for the extensible flexfield context named PackShipInstruction.

    packShipInstruction.setAttribute("_ShippingInstruction", "Hazardous Handling Required.");

    Set the context segment for Shipping Instruction.

  2. Define the public view object.

    Code

    Description

    Object getItem(Long itemId, Long orgId, def logger) {

    logger.logFiner("Entering method getItem");

    def itemPVO = context.getViewObject("oracle.apps.scm.productModel.items.publicView.ItemPVO");

    Create an instance of a public view object named Item. Specify to log at the FINER level.

    def vc = itemPVO.createViewCriteria();

    Create the view criteria object.

    def vcrow = vc.createViewCriteriaRow();

    Create the view criteria row.

    vcrow.setAttribute("InventoryItemId", itemId);

    Set the inventory item attribute to include in the filter condition and the value to use when comparing against this attribute.

    vcrow.setAttribute("OrganizationId", orgId);

    Set the organization attribute to include in the filter condition and the value to use when comparing against this attribute.

    def rowset = itemPVO.findByViewCriteria(vc, -1);

    Specify the view criteria to filter rows and query the view object.

    def item = rowset.first();

    Get the first item row that matches the condition.

    logger.logFiner("Exiting method getItem: itemNumber %s", item.getAttribute("ItemNumber"));

    Specify to log at the FINER level. Exit the log.

    The code uses parameters.

    • param itemId. Inventory item Id that identifies the item.

    • param orgId. Inventory organization Id that identifies the organization that owns the item.

Here's the same code with no comments.

def logger = context.getLogger();                                                                   
def item = getItem(inventoryItemId, orgId);                                                         
String hazardous = item.getAttribute("HazardousMaterialFlag");                                      
if( "Y".equals(hazardous) ) {                                                                       
	logger.logFinest("Found line with hazardous item %s, %s", inventoryItemId, orgId);                
	def packShipInstruction = line. getOrCreateContextRow("PackShipInstruction");                     
	packShipInstruction.setAttribute("_ShippingInstruction", "Hazardous Handling Required.");         
}
Object getItem(Long itemId, Long orgId, def logger) {
	logger.logFiner("Entering method getItem");
	def itemPVO = context.getViewObject("oracle.apps.scm.productModel.items.publicView.ItemPVO");
	def vc = itemPVO.createViewCriteria();                                                               
	def vcrow = vc.createViewCriteriaRow();                                                              
	vcrow.setAttribute("InventoryItemId", itemId);                                                       
	vcrow.setAttribute("OrganizationId", orgId);                                                         
	def rowset = itemPVO.findByViewCriteria(vc, -1);                                                     
	def item = rowset.first();                                                                           
	logger.logFiner("Exiting method getItem: itemNumber %s", item.getAttribute("ItemNumber"));           
	return item;
}