Skip to content

Commit

Permalink
Add REST method and full URL info to Log mediator message
Browse files Browse the repository at this point in the history
  • Loading branch information
sajinieKavindya committed Jan 27, 2025
1 parent e3afd33 commit 8788cdb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.synapse.mediators.AbstractMediator;
import org.apache.synapse.mediators.MediatorProperty;
import org.apache.synapse.util.InlineExpressionUtil;
import org.apache.synapse.util.logging.LoggingUtils;
import org.apache.synapse.util.xpath.SynapseExpression;
import org.jaxen.JaxenException;

Expand Down Expand Up @@ -114,6 +115,11 @@ public boolean mediate(MessageContext synCtx) {
}
}

if (this.getLogLevel() == MESSAGE_TEMPLATE) {
// Entry points info should be logged for audit logs only. Hence, the property "logEntryPointInfo"
// should be set at this point just before the audit logs and removed just after audit logs.
synCtx.setProperty(LoggingUtils.LOG_ENTRY_POINT_INFO, "true");
}
switch (category) {
case CATEGORY_INFO :
synLog.auditLog(getLogMessage(synCtx));
Expand All @@ -139,6 +145,7 @@ public boolean mediate(MessageContext synCtx) {
break;
}

synCtx.getPropertyKeySet().remove(LoggingUtils.LOG_ENTRY_POINT_INFO);
synLog.traceOrDebug("End : Log mediator");

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@

import org.apache.synapse.MessageContext;
import org.apache.synapse.SynapseConstants;

import org.apache.synapse.rest.RESTConstants;
/**
* Util class to get formatted logs for audit purposes.
*/
public class LoggingUtils {

private static final String OPEN_BRACKETS = "{";
private static final String CLOSE_BRACKETS = "}";
public static final String LOG_ENTRY_POINT_INFO = "logEntryPointInfo";

private LoggingUtils() {
// do nothing
Expand All @@ -42,7 +43,7 @@ public static String getFormattedLog(MessageContext synCtx, Object msg) {
return getFormattedLog(SynapseConstants.PROXY_SERVICE_TYPE,
artifactName.substring(SynapseConstants.PROXY_SERVICE_TYPE.length()), msg);
} else if (artifactName.startsWith(SynapseConstants.FAIL_SAFE_MODE_API)) {
return getFormattedLog(SynapseConstants.FAIL_SAFE_MODE_API,
return getFormattedLogForAPI(synCtx, SynapseConstants.FAIL_SAFE_MODE_API,
artifactName.substring(SynapseConstants.FAIL_SAFE_MODE_API.length()), msg);
} else if (artifactName.startsWith(SynapseConstants.FAIL_SAFE_MODE_INBOUND_ENDPOINT)) {
return getFormattedLog(SynapseConstants.FAIL_SAFE_MODE_INBOUND_ENDPOINT, artifactName
Expand All @@ -53,6 +54,23 @@ public static String getFormattedLog(MessageContext synCtx, Object msg) {
return msg.toString();
}

private static String getFormattedLogForAPI(MessageContext synCtx, String artifactType, Object artifactName,
Object msg) {
boolean logEntryPointInfo = Boolean.parseBoolean((String) synCtx.getProperty(LoggingUtils.LOG_ENTRY_POINT_INFO));
if (!logEntryPointInfo) {
return getFormattedLog(artifactType, artifactName, msg);
}
String name = artifactName != null ? artifactName.toString() : "";
String artifactInfo = artifactType.concat(":").concat(name);
String method = (String) synCtx.getProperty(RESTConstants.REST_METHOD);
String restFullRequestPath = (String) synCtx.getProperty(RESTConstants.REST_FULL_REQUEST_PATH);

String apiInfo = String.join(" ", artifactInfo, method != null ? method : "",
restFullRequestPath != null ? restFullRequestPath : "").trim();

return getFormattedString(apiInfo, msg);
}

public static String getFormattedLog(String artifactType, Object artifactName, Object msg) {

String name = artifactName != null ? artifactName.toString() : "";
Expand Down

0 comments on commit 8788cdb

Please sign in to comment.