SAP Cloud Platform Integration Groovy Log Beautifier
As an integration developer, when things go wrong, we have to monitor messages and analyze payloads so we can debug the message processing.
In this blog : HCI First Steps Part 6 – Identifying issues , you can find HCI’s default logging options. However, the logs which are generated during tracing operations are not persistent. Later, we came up with groovy scripts and embed them into the iFlow. For example with the script below, the response is written into the message log. The groovy script below is well known in SDN community.
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;
}
We add message log groovy script before and after the message mapping.
The groovy script inserts payloads to the message processing log. Everything looks ok so far.
However, the XML data is hard to read and usually, we use online beautifiers to validate, format and make it beautiful. This is an extra step for a busy developer.
As we know, JAVA has libraries to beautify XML so let’s use them in groovy script. We do not change the iFlow objects other than groovy script.
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.Source;
import java.io.StringWriter;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.OutputKeys;
def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String;
def messageLog = messageLogFactory.getMessageLog(message);
def mProp = message.getProperties();
String logState = mProp.get("LogState");
if(messageLog != null){
Source xmlInput = new StreamSource(new StringReader(body));
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
//initialize StreamResult with File object to save to file
StreamResult result = new StreamResult(new StringWriter());
transformer.transform(xmlInput, result);
String xmlString = result.getWriter().toString();
messageLog.setStringProperty("Logging#1", "Printing Payload As Attachment")
messageLog.addAttachmentAsString(logState, xmlString, "text/plain");
}
return message;
}
The code is very straightforward. First we read the messageLog , the message properties, message state and the payload.
In the following part, we set the indentation and indent amout:
>>>>>
transformer.setOutputProperty(OutputKeys.INDENT, “yes”);
transformer.setOutputProperty(“{http://xml.apache.org/xslt}indent-amount”, “2”);
StreamResult result = new StreamResult(new StringWriter());
transformer.transform(xmlInput,result);
>>>>
And than we convert XML to string, insert string property (“Printing Payload as Attachment”) to the message processing log and insert message payload as an attachment into the Message Processing Log.
Here is the new look of the XML message log.
Save time on your Cloud Platform Integration journey and enjoy.
Great ... 🙂
The nice output can be done in one line of Groovy! Just use
def bodyNice = XmlUtil.serialize( body );
For JSON:
import groovy.json.JsonOutput
def bodyPretty = JsonOutput.prettyPrint(body)