JSON logger in Mule 4
- March 12, 2020
Logging is useful for monitoring and troubleshooting your Mule applications and the Mule server — whether that’s recording errors raised by the application or specific details, such as status notifications, custom metadata and payloads.
The out-of-the-box logger component offered by Mule runtime is easy to use and understand, but it hardly enforces any standards and doesn’t facilitate the creation of structured logs.
In this article, we’ll learn an approach to keep the logging experience as simple as writing a one text entry, while having a full JSON structure as the output using JSON logger.
JSON logger module
The JSON logger connector looks like this in Anypoint Exchange.
You can download it from Exchange by using a MuleSoft premium or guest account in Anypoint Studio and add it in the Mule palette.
Feature of JSON logger
Below are two important features of JSON logger:
- Log Location Info
- Location object gives you everything you want to know about where you are in your application.
This is what it looks like as below:
As you can see, critical troubleshooting information such as the name of the flow (rootContainer), name of the xml configuration file (fileName), and the exact line number where the logger is located (lineInFile), can now be part of the log metadata and configured through the global config element:
Disabled fields
JSON logger provides two very distinct fields out-of-the-box:
- message:Field meant for meaningful non-sensitive functional messages such as “Request received from Customer with ID: 1234”
- content:Field meant for raw data typically useful during development and test phases (payload, attributes, vars)
If we’re able to point our development team in the right direction and have them log valuable non-sensitive data in the message field and any raw troubleshooting data into the content field, then we can leverage the “disabled fields” feature to filter certain log fields from being printed (e.g. content):
The above example will effectively tell the JSON logger that it needs to skip any field defined here. (You can add many comma-separated fields such as content and message). Taking this a step further, we could assign an environment variable (such as ${logger.disabled.fields}). In lower environments it could be null but could be set to “content” on stage and production.
How to use
Below are steps for usage:
Step 1: As you can see in the image above , once installed, the JSON logger will show in the Studio Palette as any standard component; it can also be dragged-and-dropped into the canvas.
Step 2: Configure JSON logger
In this scenario, we’re logging the current #[payload] and using the START Trace Point.
Trace Points aim to facilitate the standardization of certain “checkpoints” throughout the flow execution, which, in turn, enable more intelligent dashboards and reporting based on this data.
The current JSON logger ships with the following Trace Points:
- START (Default)
- BEFORE_REQUEST
- AFTER_REQUEST
- BEFORE_TRANSFORM
- AFTER_TRANSFORM
- END
- EXCEPTION
- FLOW
In the connector,you’ll see the following configuration by default:
- The application name is equivalent to your application name.
- The application version is equivalent to your application version.
- The environment is equivalent to your application environment.
References
- https://blogs.mulesoft.com/dev/anypoint-platform-dev/json-logging-mule-4/
- https://blogs.mulesoft.com/dev/mule-dev/logging-in-mule-using-json-logger-module/
- https://docs.mulesoft.com/mule-runtime/4.2/logging-and-debugging
— By Neelam Deshmukh