Skip to Content
Technical Articles
Author's profile photo Vinod kumar

Creating Custom SAP PI/PO Message Logs with Java Mapping for Enhanced Logging

In the realm of SAP Process Integration/Process Orchestration (PI/PO), effective message logging plays a vital role in monitoring and resolving integration issues. This article delves into the process of incorporating custom logs into SAP PI/PO message logs using Java Mapping. By following the outlined steps, you can enhance the visibility and traceability of your integration processes.

Step 1: Retrieve the Message ID
The initial step in custom logging involves extracting the message ID from the ‘in’ object. This unique identifier is crucial for monitoring and associating log entries with specific messages.

Step 2: Build the Message UUID
To ensure a distinct identifier for each message, we construct a Message UUID from the message ID using a user-defined method known as ‘constructMessageID.’ This UUID serves as a key for our custom log entries.

Step 3: Formulate the Message Key Object
Following that, we create a message key object, ‘msgKey,’ using the standard class ‘MessageKey()’ and input the UUID and message direction as parameters. This message key becomes instrumental in linking log entries to specific messages.

Step 4: Include Custom Log Entries
Moving forward, we add custom log entries to the message logs. To accomplish this, we instantiate an object for the class ‘AuditAccess’ and furnish the message key, log type (Error or Success), and the log message. This step enables the recording of crucial information about the message processing.

import java.io.OutputStream;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.io.BufferedReader;
import com.sap.aii.mapping.api.*;
import com.sap.engine.interfaces.messaging.api.*;
import com.sap.engine.interfaces.messaging.api.auditlog.*;
import com.sap.engine.interfaces.messaging.api.exception.MessagingException;
 
public class TestTransformation extends AbstractTransformation {
 
    public void transform(TransformationInput in, TransformationOutput out)
            throws StreamTransformationException {
 
         String msgID = in.getInputHeader().getMessageId();
         String msgUUID = constructMessageID(msgID);
         MessageKey msgKey = new MessageKey(msgUUID, MessageDirection.OUTBOUND);
         String outputString = "This is the output payload";
 
        
        OutputStream outputstream = out.getOutputPayload().getOutputStream();
        try {
            outputstream.write(outputString.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 

    private String constructMessageID(String msgID) {
        final String DASH = "-";
        String uuidTimeLow = msgID.substring(0, 8);
        String uuidTimeMid = msgID.substring(8, 12);
        String uuidTimeHighAndVersion = msgID.substring(12, 16);
        String uuidClockSeqAndReserved = msgID.substring(16, 18);
        String uuidClockSeqLow = msgID.substring(18, 20);
        String uuidNode = msgID.substring(20, 32);
        String UUID = uuidTimeLow + DASH + uuidTimeMid + DASH
                + uuidTimeHighAndVersion + DASH + uuidClockSeqAndReserved
                + uuidClockSeqLow + DASH + uuidNode;
        return UUID;
    }
 
   
    private void printErrorLog(String ErrorMessage, MessageKey msgKey) {
        try {
            AuditAccess msgAuditAccessor = PublicAPIAccessFactory
                    .getPublicAPIAccess().getAuditAccess();
            msgAuditAccessor.addAuditLogEntry(msgKey, AuditLogStatus.ERROR,
                    ErrorMessage);
        } catch (MessagingException e1) {
            e1.printStackTrace();
        }
    }
 

    private void printSuccessLog(String SuccessMessage, MessageKey msgKey) {
        try {
            AuditAccess msgAuditAccessor = PublicAPIAccessFactory
                    .getPublicAPIAccess().getAuditAccess();
            msgAuditAccessor.addAuditLogEntry(msgKey, AuditLogStatus.SUCCESS,
                    SuccessMessage);
        } catch (MessagingException e1) {
            e1.printStackTrace();
        }
    }
}

 

Apart from enriching message logs, it’s crucial to handle exceptions gracefully when working with Java Mapping. Here’s a guide on how to gracefully terminate mapping execution and trigger an error message in the event of an exception:

 

try {
    //mapping logic here
} catch (Exception e) {
    throw new StreamTransformationException(e.toString());
}

 

Utilizing the ‘StreamTransformationException’ provides a mechanism to halt the mapping execution and flag the message as an error. This ensures prompt identification and resolution of issues during the integration processing phase.

By adhering to these steps and incorporating best practices, you gain command over your SAP PI/PO message logs. This enhancement renders the logs more informative and actionable in the context of your integration scenarios. The improved logging capability becomes a valuable asset in upholding the reliability and performance of your SAP PI/PO environment.

Assigned Tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Vadim Klimov
      Vadim Klimov

      Hi Vinod. Thank you for writing on this topic. You may want to read through and reference this blog post - although it was published a while ago, it correlates with your blog post nicely, and it feels like a substantial part of the code provided here, originates from that blog post.

      Author's profile photo Vinod kumar
      Vinod kumar
      Blog Post Author

      Thanks Vadim, will provide the reference in the next update.