How to pass values from the POM file to the Mule logs


In 
our previous post, we learnt how we can use the Maven Resource Filtering plugin to pick up values from the pom.xml file of our app and use them in our resources files.
In this post, we will see a practical example. Using resource filtering we are going to 
  • pick up contextual information present in the pom file like app name, app version and runtime version
  • modify our log4j2.xml file so that all of our logs include this information
Let’s dive in!

Create the Mule Project

First, let’s create a new Mule Project. Then, create a new flow with an HTTP Listener for GET /logs and a Logger Processor


Enable Resource Filtering

Next, let’s modify the pom file to enable the resource filtering ONLY to our log4j2.xml file. 

<?xml version="1.0" encoding="UTF-8"?>
<project>
...
<groupId>com.mycompany</groupId>
<artifactId>pom-to-logs</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>mule-application</packaging>

<name>pom-to-logs</name>

<properties>
...
<app.runtime>4.6.1</app.runtime>
...
</properties>

<build>
<resources>
<!-- Enable filtering for specific resources -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>log4j2.xml</include>
</includes>
</resource>
<!-- Disable filtering for everything else -->
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>log4j2.xml</exclude>
</excludes>
</resource>
...
</resources>
...
</build>
...
</project>

Configure the Log4j Layout

Open the log4j2.xml file. Let’s use the JSONLayout to see the logs in JSON format and show the values from the pom file in a much cleaner way.
If you don’t know how the JSONLayout works have a look at these posts:
Here’s the log4j2.xml file with the custom properties filtered from the pom file:

<?xml version="1.0" encoding="utf-8"?>
<Configuration>

<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<JSONLayout compact="false" eventEol="true"
properties="true">
<!-- POM properties -->
<KeyValuePair key="appName" value="${project.name}" />
<KeyValuePair key="version" value="${project.version}" />
<KeyValuePair key="runtime" value="${app.runtime}" />
</JSONLayout>
</Console>
...
</Appenders>

<Loggers>
...
<AsyncRoot level="INFO">
<AppenderRef ref="Console" />
</AsyncRoot>
</Loggers>

</Configuration>

Where:
  • We’ve set up an appender for the Console with the JSONLayout
  • We’re adding custom properties to theJSONLayout with the KeyValuePair elements
  • We’re defining the placeholder to be written by maven when filtering the values from the pom file with the syntax ${project.name}, ${project.version} and ${app.runtime}
  • We’ve added the AppenderRef for the Console appender in the Loggers section
Lastly, run the project and send a request to the endpoint of the app. We will now see in the console logs like this:

{
"instant" : {
"epochSecond" : 1726761597,
"nanoOfSecond" : 463000000
},
"thread" : "[MuleRuntime].uber.03: [pom-to-logs].pom-to-logsFlow.CPU_LITE @350ae338",
"level" : "INFO",
"loggerName" : "org.mule.runtime.core.internal.processor.LoggerMessageProcessor",
"message" : "Hello Gon!",
"endOfBatch" : true,
"loggerFqcn" : "org.apache.logging.slf4j.Log4jLogger",
"contextMap" : {
"correlationId" : "37c13ab0-76a0-11ef-869d-38f9d3cde3d6",
"processorPath" : "pom-to-logsFlow/processors/0"
},
"threadId" : 48,
"threadPriority" : 5,
"appName" : "pom-to-logs",
"version" : "1.0.0-SNAPSHOT",
"runtime" : "4.6.1"
}

Notice the last three values of the JSON log message correspond to the values in the pom file
Previous Post Next Post