从log4j 1.2迁移到新的log4j2。添加到pom:
<!-- Add log4j version 2 with 1.2 API -->
<dependency>
<!-- <groupId>log4j</groupId> -->
<!-- <artifactId>log4j</artifactId> -->
<!-- <version>1.2.9</version> -->
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0-beta6</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>2.0-beta6</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.0-beta6</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
并尝试通过Spring上下文将其初始化为log4j(在log4j 1.2上完美运行)。用于此配置:
<!-- Init log4j with appropriate config according to enviroment -->
<bean id="log4jConfigurer-bean" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="org.springframework.util.Log4jConfigurer"/>
<property name="targetMethod" value="initLogging"/>
<property name="arguments">
<list>
<value>classpath:config/env/log4j-${env.variable}.xml</value>
</list>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
但是看来现在行不通了吗?我做错了什么?可能是spring config应该修改吗?对spring使用这样的依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
</exclusions>
</dependency>
Run Code Online (Sandbox Code Playgroud)
Spring提供的Log4j 1.2支持已经完成了一半,但不幸的是,您所需要的不多(您需要深入了解Log4j2 Configurator类)。
对于Web应用程序,请阅读http://logging.apache.org/log4j/2.x/log4j-web/index.html上的文档(以及文档不够用的代码),看来您可以使用Log4J2自己的配置程序执行参数替换,并将从类路径加载。
将以下内容添加到您的pom.xml中:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.0-beta6</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
在您的web.xml中,使用以下命令:
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>classloader:/config/env/log4j-${sys:env.variable}.xml</param-value>
</context-param>
<listener>
<listener-class>org.apache.logging.log4j.core.web.Log4jContextListener</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)
注意类加载器之后的/,并且Log4j2在env变量之前需要sys:前缀
* 但是,到目前为止,我还无法在Spring Log4jContextListener的直接迁移中使用它。Log4j2似乎很有希望,但是目前看来,采用率仍会保留在分支上,直到没有那么多的麻烦,并拥有更好的文档为止*
| 归档时间: |
|
| 查看次数: |
9905 次 |
| 最近记录: |