由log4j升級到log4j2的過程,因spring升級4.3.X後放棄了log4j1
maven依賴的相關包
<slf4j.version>1.7.21</slf4j.version>
<log4j.version>2.7</log4j.version>
<!--1-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<!--2-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<!--3-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<!--4-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>${log4j.version}</version>
</dependency>
<!--5-->
<!-- 橋接:告訴Slf4j使用Log4j2 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
</dependency>
<!--6-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!--7-->
<!--<dependency>-->
<!--<groupId>org.apache.logging.log4j </groupId>-->
<!--<artifactId>log4j-jul</artifactId>-->
<!--<version>${log4j.version}</version>-->
<!--</dependency>-->
<!--8-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>${log4j.version}</version>
<scope>runtime</scope>
</dependency>
<!-- apache commons-logging 實際調用slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
<scope>runtime</scope>
</dependency>
<!-- 橋接:log4j1使用Log4j2 也支持其他實現到log4j的橋接,引入不同的jar包即可-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- 日誌:end-->
需要注意的是項目中其他包用到了log4j,而不是log4j2的需要在maven依賴的包下面加上
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
web.xml中配置
<listener>
<listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
</listener>
<filter>
<filter-name>log4jServletFilter</filter-name>
<filter-class>org.apache.logging.log4j.web.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}/log4j2.xml</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
或必須在web.xml
中指定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}/log4j2.xml
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 com.auto.controller;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class AutoParamController {
private static final Logger logger = LogManager.getLogger(AutoParamController.class);
public ModelAndView commonParm(ModelAndView modelAndView, int projectId) {
logger.info("List CaseParamBean {} {}", lcbp.size(), "test");
}
}
參閱源碼:org.apache.logging.log4j.web.Log4jWebInitializerImpl.start 方法
參考官網:https://logging.apache.org/log4j/2.x/manual/webapp.html
The notes made by jwensh On March 14, 2020