From cc3abc0cad8675933561a0f5fc824f133a0a0bdb Mon Sep 17 00:00:00 2001 From: Anil Konakalla Date: Wed, 18 Dec 2024 11:58:14 -0600 Subject: [PATCH] Added message expression and updated the flow log attributes --- pom.xml | 2 +- .../FlowLogAttributesExpression.java | 60 ------------------- .../logger/api/processor/FlowLogConfig.java | 34 +++++++++++ .../config/CustomLoggerConfiguration.java | 19 +++--- ...tomLoggerAbstractNotificationListener.java | 19 +++--- ...tomLoggerPipelineNotificationListener.java | 18 +++++- 6 files changed, 70 insertions(+), 82 deletions(-) delete mode 100644 src/main/java/com/avioconsulting/mule/logger/api/processor/FlowLogAttributesExpression.java create mode 100644 src/main/java/com/avioconsulting/mule/logger/api/processor/FlowLogConfig.java diff --git a/pom.xml b/pom.xml index d083b30..1dc96ae 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ mule-custom-logger - 3.1.0-SNAPSHOT + 3.1.12-SNAPSHOT mule-extension Mule Custom Logger Mule Custom Logger module that provides standard structured logging diff --git a/src/main/java/com/avioconsulting/mule/logger/api/processor/FlowLogAttributesExpression.java b/src/main/java/com/avioconsulting/mule/logger/api/processor/FlowLogAttributesExpression.java deleted file mode 100644 index ab29037..0000000 --- a/src/main/java/com/avioconsulting/mule/logger/api/processor/FlowLogAttributesExpression.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.avioconsulting.mule.logger.api.processor; - -import org.mule.runtime.extension.api.annotation.Alias; -import org.mule.runtime.extension.api.annotation.param.Parameter; -import org.mule.runtime.extension.api.annotation.param.display.Summary; - -import java.util.Objects; - -@Alias("flow-attributes-expression") -public class FlowLogAttributesExpression { - - @Parameter - @Summary("Name of the flow to associate given expression as attributes") - private String flowName; - @Parameter - @Summary("A valid dataweave expression that resolves to a Map object with key-value pairs") - private String expressionText; - - public String getFlowName() { - return flowName; - } - - public FlowLogAttributesExpression setFlowName(String flowName) { - this.flowName = flowName; - return this; - } - - public String getExpressionText() { - return expressionText; - } - - public FlowLogAttributesExpression setExpressionText(String expressionText) { - this.expressionText = expressionText; - return this; - } - - @Override - public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; - FlowLogAttributesExpression that = (FlowLogAttributesExpression) o; - return Objects.equals(getFlowName(), that.getFlowName()) - && Objects.equals(getExpressionText(), that.getExpressionText()); - } - - @Override - public int hashCode() { - return Objects.hash(getFlowName(), getExpressionText()); - } - - @Override - public String toString() { - return "FlowLogAttributesExpression{" + - "flowName='" + flowName + '\'' + - ", attributeExpression='" + expressionText + '\'' + - '}'; - } -} diff --git a/src/main/java/com/avioconsulting/mule/logger/api/processor/FlowLogConfig.java b/src/main/java/com/avioconsulting/mule/logger/api/processor/FlowLogConfig.java new file mode 100644 index 0000000..34e2978 --- /dev/null +++ b/src/main/java/com/avioconsulting/mule/logger/api/processor/FlowLogConfig.java @@ -0,0 +1,34 @@ +package com.avioconsulting.mule.logger.api.processor; + +import org.mule.runtime.extension.api.annotation.Alias; +import org.mule.runtime.extension.api.annotation.param.Parameter; +import org.mule.runtime.extension.api.annotation.param.ParameterGroup; +import org.mule.runtime.extension.api.annotation.param.display.Summary; + +@Alias("flow-logs-config") +public class FlowLogConfig { + + @Parameter + @Summary("Name of the flow to associate given expression as attributes") + private String flowName; + + @ParameterGroup(name = "Flow Attributes") + private ExpressionText expressionText; + + public String getFlowName() { + return flowName; + } + + public FlowLogConfig setFlowName(String flowName) { + this.flowName = flowName; + return this; + } + + public ExpressionText getExpressionText() { + return expressionText; + } + + public void setExpressionText(ExpressionText expressionText) { + this.expressionText = expressionText; + } +} diff --git a/src/main/java/com/avioconsulting/mule/logger/internal/config/CustomLoggerConfiguration.java b/src/main/java/com/avioconsulting/mule/logger/internal/config/CustomLoggerConfiguration.java index 97aaeae..60ab109 100644 --- a/src/main/java/com/avioconsulting/mule/logger/internal/config/CustomLoggerConfiguration.java +++ b/src/main/java/com/avioconsulting/mule/logger/internal/config/CustomLoggerConfiguration.java @@ -4,7 +4,7 @@ import com.avioconsulting.mule.logger.api.processor.Compressor; import com.avioconsulting.mule.logger.api.processor.EncryptionAlgorithm; -import com.avioconsulting.mule.logger.api.processor.FlowLogAttributesExpression; +import com.avioconsulting.mule.logger.api.processor.FlowLogConfig; import com.avioconsulting.mule.logger.api.processor.LogProperties; import com.avioconsulting.mule.logger.internal.CustomLogger; import com.avioconsulting.mule.logger.internal.CustomLoggerOperation; @@ -31,6 +31,7 @@ import java.util.List; import java.util.Map; +import java.util.function.Function; import java.util.stream.Collectors; /** @@ -96,7 +97,7 @@ public class CustomLoggerConfiguration implements Startable, Initialisable { @NullSafe @Optional @Expression(ExpressionSupport.NOT_SUPPORTED) - private List flowLogAttributes; + private List flowLogConfigs; @Parameter @DisplayName("Flow Log Category Suffix") @@ -155,7 +156,7 @@ public class CustomLoggerConfiguration implements Startable, Initialisable { @Inject ExpressionManager expressionManager; - private Map flowLogAttributesMap; + private Map flowLogConfigMap; /** * Default constructor for auto-initialization @@ -194,12 +195,12 @@ public CustomLoggerConfiguration(CustomLoggerRegistrationService customLoggerReg private static boolean isNotificationListenerRegistered = false; - public Map getFlowLogAttributesMap() { - return flowLogAttributesMap; + public Map getFlowLogConfigMap() { + return flowLogConfigMap; } - public CustomLoggerConfiguration setFlowLogAttributes(List flowLogAttributes) { - this.flowLogAttributes = flowLogAttributes; + public CustomLoggerConfiguration setFlowLogConfigs(List flowLogConfigs) { + this.flowLogConfigs = flowLogConfigs; return this; } @@ -344,8 +345,8 @@ public void start() throws MuleException { customLoggerRegistrationService.setConfig(this); if (isEnableFlowLogs()) { classLogger.info("Flow logs enabled"); - flowLogAttributesMap = flowLogAttributes.stream().collect(Collectors - .toMap(FlowLogAttributesExpression::getFlowName, FlowLogAttributesExpression::getExpressionText)); + flowLogConfigMap = flowLogConfigs.stream().collect( + Collectors.toMap(FlowLogConfig::getFlowName, Function.identity())); synchronized (CustomLoggerConfiguration.class) { if (!isNotificationListenerRegistered) { classLogger.info("Creating and registering notification listener"); diff --git a/src/main/java/com/avioconsulting/mule/logger/internal/listeners/CustomLoggerAbstractNotificationListener.java b/src/main/java/com/avioconsulting/mule/logger/internal/listeners/CustomLoggerAbstractNotificationListener.java index f748ad1..1bcf39d 100644 --- a/src/main/java/com/avioconsulting/mule/logger/internal/listeners/CustomLoggerAbstractNotificationListener.java +++ b/src/main/java/com/avioconsulting/mule/logger/internal/listeners/CustomLoggerAbstractNotificationListener.java @@ -1,9 +1,6 @@ package com.avioconsulting.mule.logger.internal.listeners; -import com.avioconsulting.mule.logger.api.processor.AdditionalProperties; -import com.avioconsulting.mule.logger.api.processor.ExceptionProperties; -import com.avioconsulting.mule.logger.api.processor.LogProperties; -import com.avioconsulting.mule.logger.api.processor.MessageAttributes; +import com.avioconsulting.mule.logger.api.processor.*; import com.avioconsulting.mule.logger.internal.CustomLogger; import com.avioconsulting.mule.logger.internal.config.CustomLoggerConfiguration; import org.mule.runtime.api.component.location.ComponentLocation; @@ -53,10 +50,10 @@ protected void logMessage(ComponentLocation location, Event event, String logMes protected Map getFlowLogAttributes(EnrichedServerNotification notification) { Map value = emptyAttributes; - String expression = config.getFlowLogAttributesMap().get(notification.getResourceIdentifier()); - if (expression != null) { + FlowLogConfig flowLogConfig = config.getFlowLogConfigMap().get(notification.getResourceIdentifier()); + if (flowLogConfig != null) { TypedValue> evaluate = (TypedValue>) config.getExpressionManager() - .evaluate("#[" + expression + "]", + .evaluate("#[" + flowLogConfig.getExpressionText().getAttributesExpressionText() + "]", notification.getEvent().asBindingContext()); value = evaluate.getValue(); if (value == null) @@ -70,14 +67,14 @@ protected Map getFlowLogAttributes(EnrichedServerNotification no * ex: *-mq-flow will look for all the flows that ends with -mq-flow **/ else { - List> matchedEntries = config.getFlowLogAttributesMap().entrySet().stream() + List> matchedEntries = config.getFlowLogConfigMap().entrySet().stream() .filter(entry -> matchWildcard(entry.getKey(), notification.getResourceIdentifier())) .collect(Collectors.toList()); if (!matchedEntries.isEmpty()) { - expression = matchedEntries.get(0).getValue(); + flowLogConfig = matchedEntries.get(0).getValue(); TypedValue> evaluate = (TypedValue>) config .getExpressionManager() - .evaluate("#[" + expression + "]", + .evaluate("#[" + flowLogConfig.getExpressionText().getAttributesExpressionText() + "]", notification.getEvent().asBindingContext()); value = evaluate.getValue(); if (value == null) @@ -112,4 +109,4 @@ public boolean matchWildcard(String wildcardKey, String searchString) { return searchString.equals(wildcardKey); } -} +} \ No newline at end of file diff --git a/src/main/java/com/avioconsulting/mule/logger/internal/listeners/CustomLoggerPipelineNotificationListener.java b/src/main/java/com/avioconsulting/mule/logger/internal/listeners/CustomLoggerPipelineNotificationListener.java index 23ed3e0..886a9b4 100644 --- a/src/main/java/com/avioconsulting/mule/logger/internal/listeners/CustomLoggerPipelineNotificationListener.java +++ b/src/main/java/com/avioconsulting/mule/logger/internal/listeners/CustomLoggerPipelineNotificationListener.java @@ -1,12 +1,16 @@ package com.avioconsulting.mule.logger.internal.listeners; +import com.avioconsulting.mule.logger.api.processor.FlowLogConfig; import com.avioconsulting.mule.logger.internal.config.CustomLoggerConfiguration; +import org.mule.runtime.api.metadata.TypedValue; import org.mule.runtime.api.notification.PipelineMessageNotification; import org.mule.runtime.api.notification.PipelineMessageNotificationListener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.List; import java.util.Map; +import java.util.stream.Collectors; /* * Listener for Mule notifications on flow start, end and completion. @@ -37,10 +41,22 @@ public void onNotification(PipelineMessageNotification notification) { + "]"); if (config != null) { try { + String msgToAppend = ""; + List> matchedEntries = config.getFlowLogConfigMap().entrySet().stream() + .filter(entry -> matchWildcard(entry.getKey(), notification.getResourceIdentifier())) + .collect(Collectors.toList()); + if (!matchedEntries.isEmpty()) { + FlowLogConfig flowLogConfig = matchedEntries.get(0).getValue(); + TypedValue evaluate = (TypedValue) config + .getExpressionManager() + .evaluate("#[" + flowLogConfig.getExpressionText().getMessageExpressionText() + "]", + notification.getEvent().asBindingContext()); + msgToAppend = evaluate.getValue(); + } String message = "Event not processed yet, this should never be shown"; switch (Integer.parseInt(notification.getAction().getIdentifier())) { case PipelineMessageNotification.PROCESS_START: - message = "Flow [" + notification.getResourceIdentifier() + "]" + " start"; + message = "Flow [" + notification.getResourceIdentifier() + "]" + " start " + msgToAppend; break; case PipelineMessageNotification.PROCESS_COMPLETE: message = "Flow [" + notification.getResourceIdentifier() + "]" + " end";