diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/LogMediatorFactory.java b/modules/core/src/main/java/org/apache/synapse/config/xml/LogMediatorFactory.java index b9db3d7c46..e54d889a69 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/LogMediatorFactory.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/LogMediatorFactory.java @@ -68,10 +68,11 @@ public Mediator createSpecificMediator(OMElement elem, Properties properties) { // set its common attributes such as tracing etc processAuditStatus(logMediator,elem); + boolean containMessageTemplate = false; OMElement messageElement = elem.getFirstChildWithName(ELEMENT_MESSAGE_Q); if (messageElement != null && messageElement.getText() != null) { logMediator.setMessageTemplate(messageElement.getText()); - logMediator.setLogLevel(LogMediator.MESSAGE_TEMPLATE); + containMessageTemplate = true; } // Set the high level set of properties to be logged (i.e. log level) @@ -90,6 +91,10 @@ public Mediator createSpecificMediator(OMElement elem, Properties properties) { handleException("Invalid log level. Level has to be one of the following : " + "simple, headers, full, custom"); } + } else { + if (containMessageTemplate) { + logMediator.setLogLevel(LogMediator.MESSAGE_TEMPLATE); + } } // Set the log statement category (i.e. INFO, DEBUG, etc..) diff --git a/modules/core/src/main/java/org/apache/synapse/config/xml/LogMediatorSerializer.java b/modules/core/src/main/java/org/apache/synapse/config/xml/LogMediatorSerializer.java index 08e7fc1856..841d186055 100644 --- a/modules/core/src/main/java/org/apache/synapse/config/xml/LogMediatorSerializer.java +++ b/modules/core/src/main/java/org/apache/synapse/config/xml/LogMediatorSerializer.java @@ -44,7 +44,7 @@ public OMElement serializeSpecificMediator(Mediator m) { OMElement log = fac.createOMElement("log", synNS); saveTracingState(log,mediator); - if (StringUtils.isBlank(mediator.getMessageTemplate()) && mediator.getLogLevel() != LogMediator.SIMPLE) { + if (mediator.getLogLevel() != LogMediator.MESSAGE_TEMPLATE) { log.addAttribute(fac.createOMAttribute( "level", nullNS, mediator.getLogLevel() == LogMediator.HEADERS ? "headers" : diff --git a/modules/core/src/main/java/org/apache/synapse/mediators/builtin/LogMediator.java b/modules/core/src/main/java/org/apache/synapse/mediators/builtin/LogMediator.java index b313e4fc03..7daff54cbf 100644 --- a/modules/core/src/main/java/org/apache/synapse/mediators/builtin/LogMediator.java +++ b/modules/core/src/main/java/org/apache/synapse/mediators/builtin/LogMediator.java @@ -78,7 +78,7 @@ public class LogMediator extends AbstractMediator { /** The holder for the custom properties */ private final List properties = new ArrayList(); - private String messageTemplate; + private String messageTemplate = ""; /** * Logs the current message according to the supplied semantics @@ -142,6 +142,7 @@ public boolean mediate(MessageContext synCtx) { private String getLogMessage(MessageContext synCtx) { switch (logLevel) { case CUSTOM: + case MESSAGE_TEMPLATE: return getCustomLogMessage(synCtx); case SIMPLE: return getSimpleLogMessage(synCtx); @@ -149,8 +150,6 @@ private String getLogMessage(MessageContext synCtx) { return getHeadersLogMessage(synCtx); case FULL: return getFullLogMessage(synCtx); - case MESSAGE_TEMPLATE: - return processMessageTemplate(synCtx, messageTemplate); default: return "Invalid log level specified"; } @@ -158,12 +157,18 @@ private String getLogMessage(MessageContext synCtx) { private String getCustomLogMessage(MessageContext synCtx) { StringBuffer sb = new StringBuffer(); + processMessageTemplate(sb, synCtx, messageTemplate); setCustomProperties(sb, synCtx); return trimLeadingSeparator(sb); } private String getSimpleLogMessage(MessageContext synCtx) { StringBuffer sb = new StringBuffer(); + processMessageTemplate(sb, synCtx, messageTemplate); + // append separator if the message template is not empty + if (sb.length() > 0) { + sb.append(separator); + } if (synCtx.getTo() != null) sb.append("To: ").append(synCtx.getTo().getAddress()); else @@ -188,6 +193,7 @@ private String getSimpleLogMessage(MessageContext synCtx) { private String getHeadersLogMessage(MessageContext synCtx) { StringBuffer sb = new StringBuffer(); + processMessageTemplate(sb, synCtx, messageTemplate); if (synCtx.getEnvelope() != null) { SOAPHeader header = synCtx.getEnvelope().getHeader(); if (getCorrelationId(synCtx) != null) @@ -315,20 +321,17 @@ private String trimLeadingSeparator(StringBuffer sb) { } } - private String processMessageTemplate(MessageContext synCtx, String template) { - StringBuffer result = new StringBuffer(); + private void processMessageTemplate(StringBuffer stringBuffer, MessageContext synCtx, String template) { try { - result.append(InlineExpressionUtil.processInLineSynapseExpressionTemplate(synCtx, template)); + stringBuffer.append(InlineExpressionUtil.processInLineSynapseExpressionTemplate(synCtx, template)); } catch (JaxenException e) { handleException("Failed to process the message template : " + template, e, synCtx); } - setCustomProperties(result, synCtx); - return result.toString(); } @Override public boolean isContentAware() { - if (logLevel == MESSAGE_TEMPLATE) { + if (logLevel == MESSAGE_TEMPLATE || logLevel == CUSTOM) { for (MediatorProperty property : properties) { if (property.getExpression() != null && property.getExpression().isContentAware()) { return true; @@ -336,14 +339,6 @@ public boolean isContentAware() { } return InlineExpressionUtil.isInlineSynapseExpressionsContentAware(messageTemplate); } - if (logLevel == CUSTOM) { - for (MediatorProperty property : properties) { - if (property.getExpression() != null && property.getExpression().isContentAware()) { - return true; - } - } - return false; - } return true; } } diff --git a/modules/core/src/test/java/org/apache/synapse/config/xml/LogMediatorSerializationTest.java b/modules/core/src/test/java/org/apache/synapse/config/xml/LogMediatorSerializationTest.java index f135a5f2ea..60862be49a 100644 --- a/modules/core/src/test/java/org/apache/synapse/config/xml/LogMediatorSerializationTest.java +++ b/modules/core/src/test/java/org/apache/synapse/config/xml/LogMediatorSerializationTest.java @@ -161,7 +161,7 @@ private String getXmlOfMediatorScenarioTwo(String level, String seperator) { } private String getXmlOfMediatorScenarioThree(String category) { - return ""; }