Reputation: 196
I want to configure log4j2.xml file via my applicationContext.xml not web.xml. If this log4j2.xml file exist in classpath, I can't make any change in this file. So, I should configure with this applicationContext.xml.
pom.xml
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
applicationContext.xml
<bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass"
value="org.apache.logging.log4j.LogManager" />
<property name="targetMethod" value="getContext" />
<property name="arguments">
<list>
<value>false</value>
</list>
</property>
</bean>
<bean id="log4jContext" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="log4jInitialization"/>
<property name="targetMethod" value="setConfigLocation"/>
<property name="arguments">
<list>
<value>C:/projects/conf/log4j2.xml</value>
</list>
</property>
</bean>
I think LoggerContext class does not have a setConfigLocation() method because getContext() return log4j-api's LoggerContext class not log4j-core's.
How can I handle this situation? Thank you for your help.
Upvotes: 1
Views: 2816
Reputation: 196
I used code as shown below, and I succedded when I using Tomcat Server but If I use WebSphere Server, Program does not print logs into file.
<bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass"
value="org.apache.logging.log4j.core.LoggerContext" />
<property name="targetMethod" value="getContext" />
<property name="arguments">
<list>
<value>false</value>
</list>
</property>
</bean>
<bean id="log4jContext" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="log4jInitialization"/>
<property name="targetMethod" value="setConfigLocation"/>
<property name="arguments">
<list>
<!--<value>classpath:log4j2.xml</value>-->
<value>file:c:/projects/conf/log4j2.xml</value>
</list>
</property>
</bean>
Upvotes: 1