Adding Audit Log in PI Mapping UDF in PI Single Stack
Perhaps this is not a new topic , however I thought quickly consolidating all the steps required for adding a audit log entry from PI user defined function. Hope it will be helpful.
Before we go to the steps lets quickly go through the difference between Trace Vs Audit Log.
Entries added using the Trace object are added in the logs and traces, which can be viewed in NWA -> Troubleshooting -> Logs and Traces -> Log Viewer application and are not part of the Message Log often referred as Audit log of a message.
Entries added using Audit object are added in the Message Logs (Audit Logs) and are associated with a message and viewed from message monitor.
Step 1 : Download a com.sap.aii.af.ms.ifc_api.jar.jar from PI server ( you can ask your basis team to provide it) and import in your ESR namespace as imported archive object. This jar is required to access the Audit and MessageKey class in the UDF at the compile time.
Step 2: Add the imported archive in the UDF ‘Archive Used’ tab.
Step 3: Add class com.sap.engine.interfaces.messaging.api.* and com.sap.engine.interfaces.messaging.api.auditlog.* under import tab.
Step 3: Use the below code snippet to add the audit log entries in the UDF
MessageKey key = null
AuditAccess audit = null;
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 msgUUID = uuidTimeLow + DASH + uuidTimeMid + DASH + uuidTimeHighAndVersion + DASH + uuidClockSeqAndReserved + uuidClockSeqLow + DASH + uuidNode;
audit = PublicAPIAccessFactory.getPublicAPIAccess().getAuditAccess();
key = new MessageKey(msgUUID, MessageDirection.OUTBOUND);
audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, “Audit Log entry added successfully !!”);
throw new StreamTransformationException(“Exception Occured”,ex);
Thats all , now you can run a message verify if the entry ” Audit Log entry added successfully !!” in you message log/audit log.
Hope this provides a quick reference for adding audit logs.