Manuel Saucedo
Manuel Saucedo

Reputation: 525

log4j2.xml configuration from file for mule applications in mule 3.6.2

How can I get the configuration from a specific file like '/opt/applications/app1/log/config/log4j2.xml' for a mule application in mule 3.6.2. The common way is to get the configuration from a config file log4j2.xml which is stored in the resource folder, we need to read this configuration file from other external path.

Upvotes: 1

Views: 1415

Answers (1)

JhonQO
JhonQO

Reputation: 368

By default, Mule use its own log4j2 file for logging. To read log4j2.xml configuration file from external path, add the next beans in your file Application Context, For that specify the external file to be used in the context General of Mule.

Application Context:

     <bean id="loggerContext" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetClass">
          <value> org.apache.logging.log4j.LogManager</value>
        </property>
        <property name="targetMethod">
          <value>getContext</value>
        </property>
        <property name="arguments">
          <value>false</value>
        </property>
      </bean>

      <bean id="loggerContext1" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" ref="loggerContext" />
        <property name="targetMethod">
          <value>setConfigLocation</value>
        </property>
        <property name="arguments">
          <value>${log4j.external.path}</value>
        </property>
      </bean>

      <bean id="loggerContext2" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" ref="loggerContext" />
        <property name="targetMethod">
          <value>reconfigure</value>
        </property>
      </bean>

Then you need to import this context from your file Mule flow, with:

Mule Config

 <mule xmlns:https="http://www.mulesoft.org/schema/mule/https"
            xmlns="http://www.mulesoft.org/schema/mule/core"
    {..}
   <!-- Add this: -->
    <spring:beans>
        <spring:import resource="classpath*:application-context.xml" />
    </spring:beans>
    {..}
    <flow name="http-name" >
        {..}
    </flow> 
</mule> 

Upvotes: 3

Related Questions