{{announcement.body}}
{{announcement.title}}

Mule Smart Logging Connector Using XML SDK

DZone 's Guide to

Mule Smart Logging Connector Using XML SDK

This document describes demonstrating steps to build a mule soft logging connector using XML SDK instead of java SDK connector.

· Integration Zone ·
Free Resource

Preface

This document describes demonstrating steps to build a mule soft logging connector using XML SDK instead of java SDK connector, which extends mule runtime capabilities.

This approach is easier to build compared to the existing JAVA Mule SDK

SDK in Mule 4

Mule SDK connector enables reusability, offload complexity, and loosely coupled from mule project.

When we develop a Mule connector module using either the Java or XML SDKs, we are developing on top of Mule Extensions API. This API is a well-defined contract for how modules can interact with the Mule Runtime. These SDKs provide a simple layer of abstraction over the Extensions API so developers do not need to have details of how to use the API. The arc-type maven command is responsible to generate this skeleton.

XML SDK Use Case Mule-smart-logging-connector

A most useful  logging connector can be created using XML SDK to fit into any my mule soft project.

Prerequisites:

Java 1.8

Mule Runtime 4.x.x

Any point Studio 7.XX

Step 1: Generation of XML SDK Skeleton

To create the XML SDK template, we should run the following command at any desired location.

Plain Text
 




xxxxxxxxxx
1


 
1
mvn archetype:generate      -DarchetypeGroupId=org.mule.extensions -DarchetypeArtifactId=mule-extensions-xml-archetype   -DgroupId=org.mule.extension      -DartifactId=mule-smart-logging-connector   -DmuleConnectorName=mule-smart-logging-connector       



If the studio is installed with https://repository.mulesoft.org/nexus-ee/content/repositories/releases/archetype-catalog.xml, we can generate this project in the studio as well.

This will generate a skeleton with extension file module-Hello.xml, which is having below structure seen in any point studio 7.4

mule-smart

Step 2: Replace module-hello.xml with module-smart-logging-Connector.xml, with below simple self-explanatory xml code.

XML
 




xxxxxxxxxx
1
40


 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<module name="mule-smart-logging-connector"
3
        prefix="smart-logger"
4
        doc:description="This module relies in runtime provided components"
5
 
6
         xmlns="http://www.mulesoft.org/schema/mule/module"
7
        xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
8
        xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
9
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      
10
        xmlns:mule="http://www.mulesoft.org/schema/mule/core"    
11
        xmlns:httpn="http://www.mulesoft.org/schema/mule/http"  
12
         xmlns:wsc="http://www.mulesoft.org/schema/mule/wsc"   
13
        xsi:schemaLocation="
14
        http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
15
         http://www.mulesoft.org/schema/mule/module http://www.mulesoft.org/schema/mule/module/current/mule-module.xsd
16
           http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
17
           http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
18
           http://www.mulesoft.org/schema/mule/module http://www.mulesoft.org/schema/mule/module/current/mule-module.xsd
19
           http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
20
   
21
 <property name="AppName" type="string"></property>
22
<property name="ApiVersion" type="string"></property>
23
<property name="MuleEnvironment" type="string"></property>
24
    <operation name="smartlog">
25
        <parameters>
26
            <parameter name="messageid" type="string"/>
27
            <parameter name="transactionid" type="string"/>
28
             <parameter name="event" type="string"/>         
29
        </parameters>      
30
        <body>
31
 <mule:logger category="com.techstar.log" level="INFO" doc:name="Logger" message="#['{'++'"transactionid"'++ ':' ++  '"' ++ vars.transactionid ++'"'++ ',' 
32
 ++ '"messageid"'++':' ++ '"'++ vars.messageid ++ '"'++ ','
33
  ++ '"AppName"'++':' ++ '"'++ vars.AppName ++ '"'++ ','
34
   ++ '"ApiVersion"'++':' ++ '"'++ vars.ApiVersion ++ '"'++ ','
35
      ++ '"MuleEnv"'++':' ++ '"'++ vars.MuleEnvironment ++ '"'++ ','     
36
  ++ '"logevent"'++':' ++ '"'++ vars.event ++ '"'++ '}']"></mule:logger> 
37
        </body>
38
    </operation>
39
 </module>
40
 
          



Here I am declaring three input parameters message-id, transaction-id and event, and 3 configuration property parameter AppName, API version, and MuleEnv as part of my smart logger required parameter.

Finally, log all the above parameters under a package or category com.techstar.log by concatenating all the above parameters.

Step 3: To resolve a few dependencies like a mule:log etc, it may require to update or add maven plugin latest version in the pom file.

XML
 




xxxxxxxxxx
1


1
<groupId>org.apache.maven.plugins</groupId>
2
 <artifactId>maven-resources-plugin</artifactId>
3
 <version>${maven.resources.plugin.version}</version>



Step 4: Install the connector using simple maven command 

XML
 




xxxxxxxxxx
1


 
1
mvn clean install -DskipTests



This step will install the smart connector in local maven repository


Step 5:- Add this dependency in any project pom file

XML
 




xxxxxxxxxx
1


 
1
     <dependency>
2
            <groupId>org.mule.extension</groupId>
3
            <artifactId>mule-smart-logging-connector</artifactId>
4
            <version>1.0.1</version>
5
            <classifier>mule-plugin</classifier>
6
        </dependency>
7
 
          



Step 6:- complete the configuration for the smart connector in the project, which looks likes below

mule-test

mule-smart

Step 7: Once it runs in the studio, the console logs looks like below

Plain Text
 




xxxxxxxxxx
1


 
1
INFO  2020-08-20 21:32:19,862 [[MuleRuntime].uber.03: [mule-test-app].mule-test-appFlow.CPU_LITE @1a4a6d9e] [processor: smartlog/processors/0; event: 86842840-e2fe-11ea-aa04-4c1d96260e7b] com.techstar.log: {"transactionid":"814cf14a-1c54-4257-bc26-a2de7f195bf3","messageid":"0bcb76cb-a99d-4107-8423-ca1369ed75d7","AppName":"mule-test-app","ApiVersion":"v1.0","MuleEnv":"Dev","logevent":"Entering Main FLow"}



Step 8: Once it deployed in the cloud, the runtime logs look like below

runtime log

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 is made up of Java SDK, and the XML SDK. XML SDK is easier to develop without using any java code in the mule. 

Before we start developing a Mule module, we should decide which SDK and which version we are going to use, etc.

The decisions we make will depend on your particular project requirement.

References

Hope this will be useful.

Topics:
connectors, integration, logging frameworks, mule connectors, sdk, xml

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}