As the default Mule ESB Logger component, Huong Dan Java Logger will also support the Mule Expression Language (MEL) for the message. And if the user does not configure the content of the message, then the Mule Message will be returned as a string by default.
The method to build message content to log is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 |
private String getEvaluatedMessage(MuleEvent event, String message) { if (StringUtils.isEmpty(message)) { try { return event.getMessageAsString(); } catch (MuleException e) { LOGGER.error(e.getMessage(), e); return null; } } return evaluateMEL(event, message); } |
In the above method, we use a LOGGER variable to log the error in the process of obtaining the Mule Message as a string, the LOGGER variable is declared as follows:
1 |
private static final Logger LOGGER = LoggerFactory.getLogger(HuongDanJavaLoggerComponentConnector.class); |
Now we can get the message to log in the Processor section:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
package com.huongdanjava.hdjlogger; import org.apache.commons.lang3.StringUtils; import org.mule.api.MuleEvent; import org.mule.api.MuleException; import org.mule.api.annotations.Category; import org.mule.api.annotations.Config; import org.mule.api.annotations.Connector; import org.mule.api.annotations.Processor; import org.mule.api.annotations.display.FriendlyName; import org.mule.api.annotations.display.Placement; import org.mule.api.annotations.param.Default; import org.mule.api.annotations.param.Optional; import org.mule.api.expression.ExpressionManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC; import com.huongdanjava.hdjlogger.config.ConnectorConfig; import com.huongdanjava.hdjlogger.constant.LoggingLevel; import com.huongdanjava.hdjlogger.constant.Text; @Connector(name="huong-dan-java-logger-component", friendlyName="Huong Dan Java Logger") @Category(name = "org.mule.tooling.category.core", description = "Components") public class HuongDanJavaLoggerComponentConnector { private static final Logger LOGGER = LoggerFactory.getLogger(HuongDanJavaLoggerComponentConnector.class); private static final String CORRELATION_ID_ELEMENT = "correlationId"; @Config ConnectorConfig config; @Processor public String log( @Placement(group = Text.GENERIC, order = 1) @Optional String message, @Placement(group = Text.GENERIC, order = 2) @Default("INFO") @FriendlyName(Text.LEVEL) LoggingLevel loggingLevel, @Placement(group = Text.OVERRIDE, order = 1) @Optional @FriendlyName(Text.OVVERIDE_CORRELATION_ID) String globalCorrelationIdOverride, @Placement(group = Text.OVERRIDE, order = 2) @Optional @FriendlyName(Text.OVVERIDE_CATEGORY) String globalCategoryOverride, MuleEvent event) { String category = getCategory(config, globalCategoryOverride); String correlationId = getEvaluatedCorrelationId(event, config, globalCorrelationIdOverride); MDC.put(CORRELATION_ID_ELEMENT, correlationId); String evaluatedMessage = getEvaluatedMessage(event, message); return config.getCorrelationId(); } private String getEvaluatedMessage(MuleEvent event, String message) { if (StringUtils.isEmpty(message)) { try { return event.getMessageAsString(); } catch (MuleException e) { LOGGER.error(e.getMessage(), e); return null; } } return evaluateMEL(event, message); } private String getEvaluatedCorrelationId(MuleEvent event, ConnectorConfig cc, String globalCorrelationIdOverride) { if (!StringUtils.isEmpty(globalCorrelationIdOverride)) { return evaluateMEL(event, globalCorrelationIdOverride); } return evaluateMEL(event, cc.getCorrelationId()); } private String getCategory(ConnectorConfig cc, String globalCategoryOverride) { if (!StringUtils.isEmpty(globalCategoryOverride)) { return globalCategoryOverride; } return cc.getCategory(); } private String evaluateMEL(MuleEvent event, String text) { ExpressionManager em = event.getMuleContext().getExpressionManager(); return em.parse(text, event); } public ConnectorConfig getConfig() { return config; } public void setConfig(ConnectorConfig config) { this.config = config; } } |