Mule’s default Logger component is too simple to include only the log message, category, and level of that message.
And with this layout, you can see, whenever adding a new endpoint Logger in a Mule Flow, you have to configure the Logger’s Category again. What if, in the future, I want to change the Category for all endpoints of the Logger? You can imagine, right? Therefore, moving the Category to a part of Global Configuration is the first thing I want to do.
The second part, I want to add to the Global Configuration section is the configuration section for the Correlation Id in the Mule ESB. You just imagine, because Mule ESB is an Enterprise Service Bus system, so there are a lot of requests from different systems to it, Correlation Id is like an identifier to distinguish this request from another request. This will make it easy to retrieve information about the requests that are having problems in the event of a failure.
To add Category and Correlation Id to the Global Configuration section of the Huong Dan Java Logger, open the ConnectorConfig class located in the com.huongdanjava.hdjlogger.config package, delete all body content of this class and add the following lines of code. :
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 |
package com.huongdanjava.hdjlogger.config; import org.mule.api.annotations.Configurable; import org.mule.api.annotations.components.Configuration; 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 com.huongdanjava.hdjlogger.constant.Text; @Configuration(friendlyName = "Configuration") public class ConnectorConfig { @Configurable @Placement(group = Text.GENERIC, order = 1) @FriendlyName(Text.CORRELATION_ID) @Default("#[message.rootId]") private String correlationId; @Configurable @Placement(group = Text.GENERIC, order = 2) private String category; public String getCorrelationId() { return correlationId; } public void setCorrelationId(String correlationId) { this.correlationId = correlationId; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } } |
In it, the Text class is in the com.huongdanjava.hdjlogger.constant package that defines text values, which are as follows:
1 2 3 4 5 6 |
package com.huongdanjava.hdjlogger.constant; public class Text { public static final String GENERIC = "Generic"; public static final String CORRELATION_ID = "Correlation Id"; } |
As you can see in the code I just added, two new properties have been added and declared with the @Configurable annotation. This is a must have annotation to mark these configurable attributes. In the two new properties, I also use the @Placement annotation to specify the position of the two Correlation Id and Category cells. These two cells will be in the same group as Generic and the Category cell will be behind the Correlation Id.
Here, I also set the default value for Correlation Id using the annotation @Default.
The above is all I do for the Global Configuration section of Huong Dan Java Logger.
To be able to check the results, we have to solve one more problem.
Because of the default code of the Connector, I deleted, so the current code in the HuongDanJavaLoggerComponentConnector class is failing:
For the simple, I will modify to remove the error code 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 |
package com.huongdanjava.hdjlogger; 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 com.huongdanjava.hdjlogger.config.ConnectorConfig; @Connector(name="huong-dan-java-logger-component", friendlyName="Huong Dan Java Logger") @Category(name = "org.mule.tooling.category.core", description = "Components") public class HuongDanJavaLoggerComponentConnector { @Config ConnectorConfig config; /** * Custom processor * * @param friend Name to be used to generate a greeting message. * @return A greeting message */ @Processor public String greet(String friend) { /* * MESSAGE PROCESSOR CODE GOES HERE */ return config.getCorrelationId(); } public ConnectorConfig getConfig() { return config; } public void setConfig(ConnectorConfig config) { this.config = config; } } |
OK, now you can Install or Udate our connector to see the result: