Adding the Correlation Id to the log message will help us to identify which request is experiencing a problem in case our Mule ESB application fails.
I will use the Mapped Diagnostic Context of the Logging Framework to pass the Correlation Id’s information to the log message. You can refer to the concept of Mapped Diagnostic Context here.
To do this, we first declare a constant variable in the HuongDanJavaLoggerComponentConnector class, which acts as the key to pass the Correlation Id to the log message.
1 |
private static final String CORRELATION_ID_ELEMENT = "correlationId"; |
Then, in the Processor method, I would put the Correlation Id information as follows:
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 |
package com.huongdanjava.hdjlogger; import org.apache.commons.lang3.StringUtils; import org.mule.api.MuleEvent; 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.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 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); return config.getCorrelationId(); } 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; } } |