How to Install the JSON Logger


The MuleSoft JSON Logger is a custom logging mechanism used within MuleSoft applications to log information in a structured JSON format. Logging in JSON format provides a more organized and machine-readable way of tracking data, making it easier to integrate with logging platforms, such as Splunk, ELK (Elasticsearch, Logstash, and Kibana), or other monitoring tools.

In this post, we will see how to install the JSON Logger in our private Exchange and how to import it to our own Mule Project.


Installation process

  • First, create a folder in your drive where we will be downloading the JSON logger.
  • Go to the JSONLogger repository, located at this link
  • From the GitHub repo, copy the HTTPS URL


Next, open a terminal from the JSON Logger folder in your machine and clone the repo using the URL we’ve just copied
git clone https://github.com/mulesoft-consulting/json-logger.git


Modify the settings.xml of your Maven Installation

To make the JSON Logger available in your private Exchange, we need to provide credentials to publish artifacts into your Anypoint Exchange. In here, we can use a Connected App or User Credentials. For simplicity, in this post we will use the latter, so we need a local user in your Anypoint Org with Exchange Contributor permissions.

To provide these credentials, open your local maven settings.xml file, located at ~/.m2 and include a new server element:

<settings>
...
<servers>
...
<server>
<id>Exchange2</id>
<username>ANYPOINT_USERNAME</username>
<password>ANYPOINT_PASSWORD</password>
</server>
...
</server>
...
</settings>

Where you will have to replace ANYPOINT_USERNAME and ANYPOINT_PASSWORD with your current credentials.


Modify the POM

Now, head over to you JSON Logger folder and open the pom.xml file. We need to replace some versions numbers in the properties section to make it work (old versions don’t exist anymore). Replace the elements that contain in the version number the suffix 20220922 with the new suffix 20220919. More specifically, you need to modify the following properties:
  • mule.version
  • mule.sdk.version
  • mule.api.version
  • mule.metadata.version
The whole properties section should now be:
<properties>
<mule.version>4.3.0-20220919</mule.version>
<mule.weave.version>2.5.0-20220921</mule.weave.version>
<mule.sdk.version>1.3.0-20220919</mule.sdk.version>
<mule.api.version>1.3.0-20220919</mule.api.version>
<mule.metadata.version>1.3.0-20220919</mule.metadata.version>
<mule.extensions.ast.loader.version>1.2.0</mule.extensions.ast.loader.version>
<mule.extensions.maven.plugin.version>1.3.0</mule.extensions.maven.plugin.version>
<mule.app.plugins.maven.plugin.version>1.7.0</mule.app.plugins.maven.plugin.version>
<munit.extensions.maven.plugin.version>1.1.2</munit.extensions.maven.plugin.version>
<munit.version>2.3.8</munit.version>
<mule.maven.plugin.version>3.3.5</mule.maven.plugin.version>
<skipJavaTests>true</skipJavaTests>
</properties>

Deploy the JSON Logger

The github project includes a deploy script to make it easier. This script will basically inject your Anypoint Org Id (provided as an argument) into the pom file.

For that, first you need to get the ID of your Anypoint Org or the ID of the Business Group where you want the JSON Logger to be deployed. You can find it in Anypoint Platform > Access Management > Business Groups > Your BG, in the settings tab.

Once you get that, from the root folder of the JSON Logger project, run the following:

./deploy-to-exchange.sh [YOUR_BG_ID]

If everything went well you should see a Success message. To verify it was successfully uploaded, get back to Anypoint Platform and go to Exchange. Search for the JSON logger in the Business Group you selected:



Your JSON Logger is ready for you to use it in your Mule apps.

Test the JSON Logger from an app

Now let’s create a new mule app and use the JSON logger. Head over to Anypoint Studio and create a new Mule Project. From the Mule Palette drag and drop:
  • An HTTP listener - create a GET /hello endpoint
  • A Set Payload processor - Include a custom message as a response of the endpoint


Next, let’s import the JSON Logger from our org. There are two ways of doing it:

Option 1: From the Mule Palette

Click on Search in Exchange and then click on Please Login if you’re not logged in to your Anypoint Org. Provide your credentials.
Then, type json logger in search box. It should now display your JSON Logger in the left panel. 



Click on Add and Finish

Option 2: Use the Maven coordinates of the JSON Logger artifact

A quicker way to import the JSON Logger is by adding the dependency directly in the POM file. For that, open the pom.xml file of our app and, within the dependences section paste the following:

<dependency>
<groupId>[YOUR_BG_ID]</groupId>
<artifactId>json-logger</artifactId>
<version>2.1.0</version>
<classifier>mule-plugin</classifier>
</dependency>

Save the POM file and automatically Anypoint Studio will download the dependency and make it available for your project




If that does not work, double check the maven coordinates of your artifact. Go to Exchange and Open the JSON Logger asset. From there, you will find the Dependency Snippets button which tells you the coordinates of your JSON Logger.



Add the JSON Logger

Once your JSON Logger is in your Mule Palette, from its list of operations drag and drop a Logger to our flow, between the listener and the Set Payload processor.



Click on it and then create a new configuration. We will just provide hard coded values for simplicity:



Next, add a Message for your Logger



Save the file and Run the project. If everything works, the project should be compiled well and successfully deployed to Studio. After that, send a GET request to our test endpoint and see the Console. We should finally see our custom log message in JSON!


What’s Next?

Now, it’s time to have fun with the JSON Logger and explore all its functionalities. But we will leave that for the next post!
Previous Post Next Post