[SAP CPI] PAYLOAD LOGGING USING GROOVY SCRIPTS – PART 1

Hello guys, in this article I want to how how to log payload from sender during development at run time by using a script called Groovy. Here, we go.

Example, we have a integration flow as :

And we want to collect all data which got from ODATA after Request Reply to know how many data. How do we do that ?

In CPI, we have a method to do this. By using Groovy script, we can log all data into log system during development runtime. Content of Groovy script look like

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
    def body = message.getBody(java.lang.String) as String;
    def messageLog = messageLogFactory.getMessageLog(message);
    if(messageLog != null){
        messageLog.setStringProperty("Logging#1", "Printing Payload As Attachment")
        messageLog.addAttachmentAsString("ResponsePayload:", body, "text/plain");
     }
    return message;
}

In this script, have 3 critical component

def messageLog = messageLogFactory.getMessageLog(message)Get access to the Message Processing Log
messageLog.setStringProperty(“Logging#1”, “Printing Payload As Attachment”)Write String Property to the processing log.
In this, property Logging#1 written with content :Printing Payload As Attachment. You can also set this to the Header, Property, Or Payload if You require
messageLog.addAttachmentAsString(“ResponsePayload:”, body, “text/plain”);Write attachment to the message processing log. Message payload inserted as an attachment into the Message Processing Log.

Apply this script into integration flow above. We have new integration flow as :

Double click to Groovy Script and input code above

Call integration flow from Postman and check Log, we can see

Check Payload, we can get data in processing log as :

Summary

In this article, I showed how to write data payload into processing log. This method useful in case we want to debug integration flow step by step and see data every step. Thanks for your reading and any question, kindly leave your comment in below.

Thanks.

Joseph.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.