Java - 如何配置 log4j2.xml
Java - How to configure log4j2.xml
我想通过 applicationContext.xml 而不是 web.xml 配置 log4j2.xml 文件。如果此 log4j2.xml 文件存在于 classpath 中,我无法对此文件进行任何更改。所以,我应该配置这个 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>
我认为 LoggerContext class 没有 setConfigLocation() 方法,因为 getContext() return log4j-api 的 LoggerContext class 不是 log4j-core 的。
我该如何处理这种情况?感谢您的帮助。
我使用了如下所示的代码,当我使用 Tomcat 服务器时我成功了,但是如果我使用 WebSphere 服务器,程序不会将日志打印到文件中。
<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>
我想通过 applicationContext.xml 而不是 web.xml 配置 log4j2.xml 文件。如果此 log4j2.xml 文件存在于 classpath 中,我无法对此文件进行任何更改。所以,我应该配置这个 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>
我认为 LoggerContext class 没有 setConfigLocation() 方法,因为 getContext() return log4j-api 的 LoggerContext class 不是 log4j-core 的。
我该如何处理这种情况?感谢您的帮助。
我使用了如下所示的代码,当我使用 Tomcat 服务器时我成功了,但是如果我使用 WebSphere 服务器,程序不会将日志打印到文件中。
<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>