
By default SAP provides tools necessary to create custom audit log entries, when developing custom communication channel modules. In this case you can use related classes of standard XPI Adapter Library included in SAP NWDS. But what if we want to use this tool to create log entries during java-proxy execution? To be able to do that we need to create AuditAccess object which have methods that can be used to create audit log entries. You can create this object using following code:
AuditAccess auditAccess = PublicAPIAccessFactory.getPublicAPIAccess().getAuditAccess();
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.sap.engine.interfaces.messaging.api.MessageDirection;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;
public class MessageMonitor {
private Method mtdAddAuditLogEntry;
private Object objAuditAccess;
private Class clsMessageKey;
private Class clsLogStatus;
private Object objMessageKey;
public MessageMonitor(String msgId, MessageDirection msgDirection) throws NamingException, InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException {
Context context = new InitialContext();
Class clsAPIPublick = context.lookup("com.sap.engine.interfaces.messaging.api.public").getClass();
Object objAPIPublick = clsAPIPublick.newInstance();
Method mtdGetAuditAccess = clsAPIPublick.getDeclaredMethod("getAuditAccess");
objAuditAccess = mtdGetAuditAccess.invoke(objAPIPublick);
Class clsAuditAccess = objAuditAccess.getClass();
for (Method mtd : clsAuditAccess.getMethods()) {
for (Class cls : mtd.getParameterTypes()) {
if (cls.getCanonicalName().equals("com.sap.engine.interfaces.messaging.api.MessageKey")) {
clsMessageKey = cls;
} else if (cls.getCanonicalName().equals("com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus")) {
clsLogStatus = cls;
}
}
if (clsMessageKey != null && clsAuditAccess != null) break;
}
mtdAddAuditLogEntry = clsAuditAccess.getMethod("addAuditLogEntry", new Class[]{clsMessageKey, clsLogStatus, String.class});
Class clsMessageDirection = null;
outer: for (Constructor cnstr : clsMessageKey.getConstructors()) {
for (Class cls : cnstr.getParameterTypes()) {
if (cls.getCanonicalName().equals("com.sap.engine.interfaces.messaging.api.MessageDirection")) {
clsMessageDirection = cls;
break outer;
}
}
}
Constructor cnstrMessageDirection = clsMessageKey.getConstructor(new Class[]{String.class, clsMessageDirection});
Field fldMessageDirection = clsMessageDirection.getField(msgDirection.toString());
objMessageKey = cnstrMessageDirection.newInstance(new Object[]{new String(msgId), fldMessageDirection.get(clsMessageDirection)});
}
public void addLogEntry(AuditLogStatus logStatus, String logMsg) throws SecurityException, NoSuchMethodException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Field fldLogStatus;
if (logStatus.toString().equals("S")) {
fldLogStatus = clsLogStatus.getField("SUCCESS");
} else if (logStatus.toString().equals("W")) {
fldLogStatus = clsLogStatus.getField("WARNING");
} else {
fldLogStatus = clsLogStatus.getField("ERROR");
}
mtdAddAuditLogEntry.invoke(objAuditAccess, new Object[]{objMessageKey, fldLogStatus.get(clsLogStatus), new String(logMsg)});
}
}
ProviderXIMessageContext context = ProviderXIMessageContext.getInstance()
String msgId = context.getRequestMessageID().toString();ProviderXIMessageContext context = ProviderXIMessageContext.getInstance();
String msgId = context.getRequestMessageID().toString();
MessageMonitor monitor = new MessageMonitor(msgId, MessageDirection.INBOUND);
monitor.addLogEntry(AuditLogStatus.ERROR, "Its my custom Error message log");
monitor.addLogEntry(AuditLogStatus.WARNING, "Its my custom Warning message log");
monitor.addLogEntry(AuditLogStatus.SUCCESS, "Its my custom Success message log ");
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 60 | |
| 33 | |
| 23 | |
| 21 | |
| 19 | |
| 16 | |
| 16 | |
| 15 | |
| 15 | |
| 10 |