由log4j升级到log4j2的过程,因spring升级4.后放弃了log4j1
maven依赖的相关包
<>1.7.21</>
<>2.7</>
<!--1-->
<dependency>
<groupId>.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${}</version>
</dependency>
<!--2-->
<dependency>
<groupId>.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${}</version>
</dependency>
<!--3-->
<dependency>
<groupId>.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>${}</version>
</dependency>
<!--4-->
<dependency>
<groupId>.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>${}</version>
</dependency>
<!--5-->
<!-- 桥接:告诉Slf4j使用Log4j2 -->
<dependency>
<groupId>.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${}</version>
</dependency>
<!--6-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${}</version>
</dependency>
<!--7-->
<!--<dependency>-->
<!--<groupId>.log4j </groupId>-->
<!--<artifactId>log4j-jul</artifactId>-->
<!--<version>${}</version>-->
<!--</dependency>-->
<!--8-->
<dependency>
<groupId>.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>${}</version>
<scope>runtime</scope>
</dependency>
<!-- apache commons-logging 实际调用slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${}</version>
<scope>runtime</scope>
</dependency>
<!-- 桥接:log4j1使用Log4j2 也支持其他实现到log4j的桥接,引入不同的jar包即可-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${}</version>
</dependency>
<!-- 日志:end-->
需要注意的是项目中其他包用到了log4j,而不是log4j2的需要在maven依赖的包下面加上
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
中配置
<listener>
<listener-class>..Log4jServletContextListener</listener-class>
</listener>
<filter>
<filter-name>log4jServletFilter</filter-name>
<filter-class>..Log4jServletFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>log4jServletFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>classpath:/env/${env-dir}/</param-value>
</context-param>
这里需要提示下,如果加上了下面的内容(会有配置文件不生效的情况)
<context-param>
<param-name>isLog4jContextSelectorNamed</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>log4jContextName</param-name>
<param-value>appWithJndiSelector</param-value>
</context-param>
-
如果
isLog4jContextSelectorNamed
是true
,则必须指定log4jContextName
或必须在中指定
appWithJndiSelector
(名字随意,测试过好像没什么重要作用);否则,会报错。在这种情况下也应该指定log4jConfiguration
,并且必须是 configuration 文件的有效 URI,否则配置无效不可用;但是,此参数不是必需的;这里需要注意的是,log4j2的配置文件,项目打包的之后一定要放到classes
目录下,且文件名开头为log4j2
或log4j2-
,不让会报错:ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console
-
如果
isLog4jContextSelectorNamed
不是true,则可以选择指定log4jConfiguration
,并且必须是 configuration 文件的有效 URI 或路径,或者以classpath:
开头,以表示可在 classpath 上找到的 configuration 文件。如果没有此参数,Log4j 将使用标准机制来定位 configuration 文件。如果使用mvn -P dev
传递指定配置文件,可以这么配置classpath:/env/${env-dir}/
maven 配置
<profiles>
<profile>
<id>dev</id>
<properties>
<env-dir>dev</env-dir>
</properties>
</profile>
<profiles>
- 默认情况下,Log4j 2 使用
ServletContext
的context name作为LoggerContext name,并使用标准 pattern 来定位 Log4j configuration 文件。您可以使用三个 context 参数来控制此行为。第一个isLog4jContextSelectorNamed
指定是否应使用JndiContextSelector
选择 context。如果未指定isLog4jContextSelectorNamed
或true
以外的任何内容,则假定为false
。
项目中使用
package ;
import .;
import .;
public class AutoParamController {
private static final Logger logger = ();
public ModelAndView commonParm(ModelAndView modelAndView, int projectId) {
("List CaseParamBean {} {}", (), "test");
}
}
参阅源码:.. 方法
参考官网:/log4j//manual/
The notes made by jwensh On March 14, 2020