Log Forwarding Connector Mule XML-SDK
This document describes demonstrating steps to build a muleSoft logging connector using XML SDK instead of Java SDK.
Join the DZone community and get the full member experience.
Join For FreePreface
This document describes demonstrating steps to build a muleSoft logging connector using XML SDK instead of Java SDK, to forward the logs into Splunk, ELK, or any other log collector API to store and manage the logs into a centralized location. Similar to Java SDK XML SDK also extends mule runtime capabilities.
This approach is easier to build compared to the existing complex JAVA Mule SDK
SDK in Mule 4
https://dzone.com/articles/mule-smart-logging-connector-using-xml-sdk
Step 1: Generation of XML SDK Skeleton
To create the XML SDK template, we should run the following command at any desired location.
mvn archetype:generate -DarchetypeGroupId=org.mule.extensions -DarchetypeArtifactId=mule-extensions-xml-archetype -DarchetypeVersion=1.2.0 -DgroupId=org.mule.extension -DartifactId= mule-smart-logger-DmuleConnectorName=mule-smart-logger
Step 2: Replace Module-Hello.XML With Module-Smart-Log-Forwarder.XML, With Below Simple Self-Explanatory XML Code
xxxxxxxxxx
<module name="Mule-Smart-Log-Connector"
prefix="Log-Forword"
doc:description="This module relies in runtime provided components"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/module"
xmlns:mule="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:tns="http://www.mulesoft.org/schema/mule/Log-Forword"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/module http://www.mulesoft.org/schema/mule/module/current/mule-module.xsd
http://www.mulesoft.org/schema/mule/module http://www.mulesoft.org/schema/mule/module/current/mule-module.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/Log-Forword http://www.mulesoft.org/schema/mule/Log-Forword/current/mule-module-hello.xsd">
<property name="AppName" type="string"/>
<property name="ApiVersion" type="string"/>
<property name="MuleEnvironment" type="string"/>
<http:request-config name="httpreq-config" basePath="/">
<http:request-connection protocol="HTTP" host="log-collectror-app.us-e2.cloudhub.io" />
</http:request-config>
<operation name="Log-Info">
<parameters>
<parameter name="messageid" type="string"/>
<parameter name="transactionid" type="string"/>
<parameter name="message" type="string"/>
</parameters>
<body>
<mule:async doc:name="Async" >
<mule:set-variable value="#['{' ++ '"info"' ++ ':'++
'{'++'"transactionid"'++ ':' ++ '"' ++ vars.transactionid ++'"'++ ','
++ '"messageid"'++':' ++ '"'++ vars.messageid ++ '"'++ ','
++ '"AppName"'++':' ++ '"'++ vars.AppName ++ '"'++ ','
++ '"ApiVersion"'++':' ++ '"'++ vars.ApiVersion ++ '"'++ ','
++ '"MuleEnv"'++':' ++ '"'++ vars.MuleEnvironment ++ '"'++ ','
++ '"logMessage"'++':' ++ '"'++ vars.message ++ '"'++ '}'++'}']"
doc:name="SetVariable" variableName="logdata" mimeType="application/json"/>
<mule:logger category="com.techstar.log" level="INFO" doc:name="Logger" message="#[vars.logdata]" />
<mule:set-payload value="#[vars.logdata]" doc:name="SetPayload" mimeType="application/json"/>
<http:request config-ref="httpreq-config" path="/info" method="POST" />
</mule:async>
</body>
</operation>
<operation name="Log-Error">
<parameters>
<parameter name="messageid" type="string"/>
<parameter name="transactionid" type="string"/>
<parameter name="message" type="string"/>
</parameters>
<body>
<mule:async doc:name="Async" >
<mule:set-variable value="#['{' ++ '"error"' ++ ':'++
'{'++'"transactionid"'++ ':' ++ '"' ++ vars.transactionid ++'"'++ ','
++ '"messageid"'++':' ++ '"'++ vars.messageid ++ '"'++ ','
++ '"AppName"'++':' ++ '"'++ vars.AppName ++ '"'++ ','
++ '"ApiVersion"'++':' ++ '"'++ vars.ApiVersion ++ '"'++ ','
++ '"MuleEnv"'++':' ++ '"'++ vars.MuleEnvironment ++ '"'++ ','
++ '"logMessage"'++':' ++ '"'++ vars.message ++ '"'++ '}'++'}' ]"
doc:name="SetVariable" variableName="logdata" mimeType="application/json"/>
<mule:logger category="com.techstar.log" level="ERROR" doc:name="Logger" message="#[vars.logdata]"/>
<mule:set-payload value="#[vars.logdata]" doc:name="SetPayload" mimeType="application/json"/>
<http:request config-ref="httpreq-config" path="/error" method="POST" />
</mule:async>
</body>
</operation>
</module>
Step 3: Replace Module-Hello.Xml With Module-Smart-Log-Forwarder.Xml, With Below Simple Self-Explanatory Xml Code
Step 4: Add This Dependency in Any Project POM File
xxxxxxxxxx
<dependency>
<groupId>org.mule.extension</groupId>
<artifactId>mule-smart-logger</artifactId>
<version>1.0.17-SNAPSHOT</version>
<classifier>mule-plugin</classifier>
</dependency>
Step 5: Configurations
Info Log
INFO 2020-08-23 07:45:45,881 [[MuleRuntime].uber.08: [mule-test-app].log-info/processors/0 @187752a6] [processor: log-info/processors/0/processors/1; event: 8da2f720-e4e6-11ea-a1c7-4c1d96260e7b] com.techstar.log: {"info":{"transactionid":"d839e2e3-65d0-4bfe-8403-466fb22d1cf4","messageid":"cc517773-e99a-41a7-88ac-0448f987af7e","AppName":"mule-test-app-guru","ApiVersion":"v1.0","MuleEnv":"Dev","logMessage":"Entering Main Flow"}}
Error Log
ERROR 2020-08-23 07:45:45,917 [[MuleRuntime].uber.05: [mule-test-app].log-error/processors/0 @3c1f5d7] [processor: log-error/processors/0/processors/1; event: 8da2f720-e4e6-11ea-a1c7-4c1d96260e7b] com.techstar.log: {"error":{"transactionid":"d839e2e3-65d0-4bfe-8403-466fb22d1cf4","messageid":"cc517773-e99a-41a7-88ac-0448f987af7e","AppName":"mule-test-app-guru","ApiVersion":"v1.0","MuleEnv":"Dev","logMessage":"At Error handler"}}
Anypoint Cloud Hub Logs
At the Log Collector App
Limitations:
The community edition having limited functionality available for development.
Final Conclusion:
The Mule SDK allows MuleSoft developers to extend the Mule runtime with good features. This made up of Java SDK, and the XML SDK. XML SDK is easier to develop without using any java code in the mule.
The decisions we make will depend on your particular project requirement.
Hope this will be useful!
Opinions expressed by DZone contributors are their own.
Comments