Loading

How to use Properties in log4j2 Configuration File

Publiseringsdato: Aug 4, 2025
Oppgave

GOAL

This knowledge article is to show how to use system properties in log4j2 configuration files. Please note that you cannot use the Mule application properties file for this purpose since log4j2 will get initialized before the Mule application properties file.

One may want to minimize the effort of changing the application's log4j2.xml configurations per environment. For example, we want to change the log file location between production and development environments.
Trinn
We can define the file path as property 'log4j2.mycustompath' to change it per each environment as follows:
<RollingFile name="file" fileName="${sys:log4j2.mycustompath}${sys:file.separator}logs${sys:file.separator}demo-amq.log" 
                 filePattern="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}demo-amq-%i.log" >

We can also define AsyncLogger for a particular package log.packagename and log level log.level per different environment as follows:
<AsyncLogger name="${sys:log.packagename}" level="${sys:log.level}"/>

Since we cannot use application properties, we can pass them as JVM arguments (system properties) to the JVM at startup. Alternatively, we can define them in wrapper.conf.  

For more detail please refer to How can I set Mule and Java system properties at startup?

For CloudHub Applications you can define the following in application properties under Anypoint platform -> Runtime Manager -> Application -> Settings -> Properties tab:
  • log4j2.mycustompath=/tmp/in
  • log.level=TRACE
  • log.packagename=com.mulesoft.extension.mq
Please bear in mind that you need to check Disable CloudHub logs for this option to work, please refer to CloudHub Custom Log Appender document for more details. 

For Studio we can add following JVM arguments:
  • -Dlog4j2.mycustompath=/tmp/in
  • -Dlog.level=TRACE
  • -Dlog.packagename=com.mulesoft.extension.mq

For Mule startup to pass it to Mule command you can use the following:
  • -M-Dlog4j2.mycustompath=/tmp/in
  • -M-Dlog.level=TRACE
  • -M-Dlog.packagename=com.mulesoft.extension.mq
Knowledge-artikkelnummer

001114527

 
Laster
Salesforce Help | Article