About

CPI Groovy Examples

Go to List of Examples

MPL Logging Based on Flow log level

Please see "MPL Payload Log" for a simpler example

This enables controlling log behavior when the flow is already deployed.

It helps if you want to keep logging steps in the flow for development but avoid performance problems in production. You can also enable logging in production for gathering production data or debugging.

You can set your flows in development to Debug and see logs for every message.

Here is the documentation for Setting Log Levels

Script

Try it on Groovy IDE
import com.sap.gateway.ip.core.customdev.util.Message;
import groovy.xml.*;
import java.util.HashMap;

def Message processData(Message message) {
    def body = message.getBody(java.lang.String) as String;
    
    // Get LogLevel of the artifact
    def map = message.getProperties();
	def logConfig = map.get("SAP_MessageProcessingLogConfiguration");
	def logLevel = (String) logConfig.logLevel;
	
    def messageLog = messageLogFactory.getMessageLog(message);
    if(messageLog != null){
        if(logLevel.equals("DEBUG") || logLevel.equals("TRACE")) {
            messageLog.setStringProperty("Logging", "Printing Payload As Attachment");
            messageLog.addAttachmentAsString("Outgoing", body , "text/plain");
        } 
    }

    return message;
}