Deep Dive into MuleSoft's JSON Logger

Deep Dive into MuleSoft's JSON Logger

Logging, often underestimated yet crucial, plays a pivotal role in any application. Its functions range from tracking transactions and debugging errors to creating dashboards and monitoring various application processes. MuleSoft adopts the log4j2 logging standard for event logging. Adhering to best practices involves incorporating essential elements such as transaction ID, timestamp, log level and pertinent messages for comprehensive information. In this article, we'll explore an alternative logging approach using a connector: JSON-Logger.

The JSON logger, as implied by its name, outputs logs in JSON format, proving particularly beneficial for integration with external log analytic tools like ELK or Splunk. It captures pertinent information and facilitates customization of content and other fields. Additionally, the JSON logger supports the capability to send data to AMQ or JMS. To incorporate a JSON logger into your application, follow the detailed steps provided below. 

JSON logger module

Let's start with the initial step – cloning the repository to your local system. As the JSON logger is a custom connector not offered by MuleSoft, we must push the logger to Anypoint Exchange for utilization.

GitLab Repository
  • Open the Json-logger -> pom.xml file with a text editor.
  • Replace the ORG_ID_TOKEN value within the <groupId> tag with the Organization ID of the Business Group where you intend to publish the asset. To acquire this information, please consult the following article:
https://help.mulesoft.com/s/article/How-to-know-my-Organization-ID-Org-ID-on-the-Anypoint-Platform 
  • Open the template-files folder and further open settings.xml file with a text editor.
  • In the server section, replace the anypoint-exchange-v3 information with the credentials you use to login into Anypoint Platform. 
Settings.xml
  • After entering the credentials, navigate to the cloned directory and execute the shell script with the Organization ID as a parameter. The following script will be responsible for pushing your connector to MuleSoft Anypoint business exchange. 
./deploy-to-exchange.sh <org id>  
  • The JSON Logger connector looks like below in Anypoint Exchange. 
JSON Logger

You can download it from exchange by using MuleSoft premium or guest account in Anypoint Studio and add it in Mule palette. 

JSON Logger available in Mule palette

How to Use  

As depicted in the image above, after installation, the JSON logger will appear in the Studio Palette like any standard component. You can easily drag-and-drop it onto the canvas.

Code snippet

Configure JSON Logger as shown below.

JSON Logger Configuration

In the given situation, we log the current #[payload] and employ the START Trace Point. 

Trace Points aim to systematize particular 'checkpoints' in the flow execution, contributing to the establishment of more sophisticated dashboards and reporting using the collected data. 

The existing JSON logger includes the following Trace Points: 

  • START (Default) 
  • BEFORE_REQUEST 
  • AFTER_REQUEST 
  • BEFORE_TRANSFORM 
  • AFTER_TRANSFORM 
  • END 
  • EXCEPTION 
  • FLOW 
Configuration of trace points

In the connector, you will see the following configuration by default.

Default Configuration

 An Example entry by JSON logger looks like shown below.

JSON Logger example

Conclusion 

In this blog, we explored the significance of logging in applications, especially through the prism of JSON Logger in MuleSoft, underscores its pivotal role in software development. Logging not only facilitates issue tracking and troubleshooting but also offers valuable insights into application performance, user behavior, and system interactions. The adoption of JSON Logger in MuleSoft brings a structured and flexible approach to logging, enabling developers to efficiently capture, analyze, and visualize logs. In the dynamic landscape of modern software development, robust logging practices are imperative for constructing resilient, scalable, and maintainable applications. 

Mulecraft Footer