Skip to content

Commit

Permalink
Added message expression and updated the flow log attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Anil Konakalla authored and Anil Konakalla committed Dec 18, 2024
1 parent 36241a5 commit cc3abc0
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 82 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>

<artifactId>mule-custom-logger</artifactId>
<version>3.1.0-SNAPSHOT</version>
<version>3.1.12-SNAPSHOT</version>
<packaging>mule-extension</packaging>
<name>Mule Custom Logger</name>
<description>Mule Custom Logger module that provides standard structured logging</description>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +31,7 @@

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -96,7 +97,7 @@ public class CustomLoggerConfiguration implements Startable, Initialisable {
@NullSafe
@Optional
@Expression(ExpressionSupport.NOT_SUPPORTED)
private List<FlowLogAttributesExpression> flowLogAttributes;
private List<FlowLogConfig> flowLogConfigs;

@Parameter
@DisplayName("Flow Log Category Suffix")
Expand Down Expand Up @@ -155,7 +156,7 @@ public class CustomLoggerConfiguration implements Startable, Initialisable {

@Inject
ExpressionManager expressionManager;
private Map<String, String> flowLogAttributesMap;
private Map<String, FlowLogConfig> flowLogConfigMap;

/**
* Default constructor for auto-initialization
Expand Down Expand Up @@ -194,12 +195,12 @@ public CustomLoggerConfiguration(CustomLoggerRegistrationService customLoggerReg

private static boolean isNotificationListenerRegistered = false;

public Map<String, String> getFlowLogAttributesMap() {
return flowLogAttributesMap;
public Map<String, FlowLogConfig> getFlowLogConfigMap() {
return flowLogConfigMap;
}

public CustomLoggerConfiguration setFlowLogAttributes(List<FlowLogAttributesExpression> flowLogAttributes) {
this.flowLogAttributes = flowLogAttributes;
public CustomLoggerConfiguration setFlowLogConfigs(List<FlowLogConfig> flowLogConfigs) {
this.flowLogConfigs = flowLogConfigs;
return this;
}

Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -53,10 +50,10 @@ protected void logMessage(ComponentLocation location, Event event, String logMes

protected Map<String, String> getFlowLogAttributes(EnrichedServerNotification notification) {
Map<String, String> value = emptyAttributes;
String expression = config.getFlowLogAttributesMap().get(notification.getResourceIdentifier());
if (expression != null) {
FlowLogConfig flowLogConfig = config.getFlowLogConfigMap().get(notification.getResourceIdentifier());
if (flowLogConfig != null) {
TypedValue<Map<String, String>> evaluate = (TypedValue<Map<String, String>>) config.getExpressionManager()
.evaluate("#[" + expression + "]",
.evaluate("#[" + flowLogConfig.getExpressionText().getAttributesExpressionText() + "]",
notification.getEvent().asBindingContext());
value = evaluate.getValue();
if (value == null)
Expand All @@ -70,14 +67,14 @@ protected Map<String, String> getFlowLogAttributes(EnrichedServerNotification no
* ex: *-mq-flow will look for all the flows that ends with -mq-flow
**/
else {
List<Map.Entry<String, String>> matchedEntries = config.getFlowLogAttributesMap().entrySet().stream()
List<Map.Entry<String, FlowLogConfig>> 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<Map<String, String>> evaluate = (TypedValue<Map<String, String>>) config
.getExpressionManager()
.evaluate("#[" + expression + "]",
.evaluate("#[" + flowLogConfig.getExpressionText().getAttributesExpressionText() + "]",
notification.getEvent().asBindingContext());
value = evaluate.getValue();
if (value == null)
Expand Down Expand Up @@ -112,4 +109,4 @@ public boolean matchWildcard(String wildcardKey, String searchString) {
return searchString.equals(wildcardKey);
}

}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -37,10 +41,22 @@ public void onNotification(PipelineMessageNotification notification) {
+ "]");
if (config != null) {
try {
String msgToAppend = "";
List<Map.Entry<String, FlowLogConfig>> 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<String> evaluate = (TypedValue<String>) 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";
Expand Down

0 comments on commit cc3abc0

Please sign in to comment.