Create the Mulesoft Domain
- Create a standard Mulesoft Domain via the wizard in Anypoint Studio
- Update the Mule-domain-config.xml with the following snippet
1 2 3 4 5 6 7
<spring:beans> <spring:bean id="propertyConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> <spring:property name="location" value="test_${MULE_ENV}.properties"/> <spring:property name="ignoreUnresolvablePlaceholders" value="true"/> <spring:property name="ignoreResourceNotFound" value="true"/> </spring:bean> </spring:beans>
The bean will read in the property file (Name:Value)
The location property has ${MULE_ENV} and this is used at run time to inject the environment value - Create your environment property files under src/main/resources. My example I have two and the name pattern is app_${env}.properties:
- test_dev.properties
1 2
simple_log4j_example_log_level=INFO app_2_log_level=DEBUG
- test_prod.properties
1 2
simple_log4j_example_log_level=INFO app_2_log_level=INFO
Create a Simple Mulesoft Application
This step will be creating a simple Mulesoft project that references the above domain. The application will contains a single flow that listens on port 8081(http://localhost:8081/test) and returns the current log level and logs both the simple_log4j_example_log_level and the app_2_log_level from the property file specified at run time.
The following edits have been made for this:
The change required to override the log4j configuration is done on the log4j2.xml in src/main/resources/ by updating the AsyncRoot to reference the property name from the domain property file:1 2 3 | <AsyncRoot level="${simple_log4j_example_log_level}"> <AppenderRef ref="file" /> </AsyncRoot> |
Pulling this all together
Inside Anypoint Studio you will need to update the Run Configuration to include the following VM argument:
- -DMULE_ENV
- To use the dev properties file it would be: -DMULE_ENV=dev
- To use the prod properties file it would be: -DMULE_ENV=prod
This argument will be used by the domain at startup to pull in the correct properties file.
Once your application is up and running you can go to http://localhost:8081/test and see the response of the current log level for the simple_log4j_example_log_level. The log connectors can be changed to validate the properties file.
This is one of many ways this could be managed and I hope this helps others keep their logs clear of information that shouldn't be there.
Helpful Links
- My sample code can be found here if anyone wants to look at it or expand on it:
- Other Mulesoft Log4J configuration settings to think about
- Shared Resources in Mulesoft
No comments:
Post a Comment