本文介紹了在Spring Boot Web應用程序中使用橋JAR從log4j 1.x遷移到log4j 2.x時無法生成日志的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在嘗試從log4j1.x遷移到log4j2.x。
Followed this link - https://logging.apache.org/log4j/2.x/manual/migration.html
但我沒有看到更改后會生成日志。我想不出我錯過了什么。
以下是詳細信息-
現有的log4j版本-
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
替換為-
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.6.2</version>
</dependency>
我可以看到log4j-1.2.17.jar被這四個JAR替換-
-
log4j-jcl2.6.2.jar
log4j-core-2.1.jar
log4j-api-2.1.jar
log4j-1.2-api-2.6.2.jar
這是現有的配置文件(文件名/usr/local/log4j.properties)-
log4j.rootLogger=INFO, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=/var/log/access.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
已替換此系統屬性值-
logging.config=/usr/local/log4j.properties
使用這兩行
log4j1.compatibility=true
log4j.configuration=/usr/local/log4j.properties
推薦答案
Spring Boot在非常早期的階段重新配置您的日志框架。這就是為什么在應用程序啟動期間,您的外部log4j.properties
文件會被Spring替換。
如果不提供logging.config
屬性,將使用固定的類路徑資源列表(參見list)或應用默認配置。
在最近的Log4j版本上,您只需設置系統屬性:
logging.config=/usr/local/log4j.properties
log4j.configurationFactory=org.apache.log4j.config.Log4j1ConfigurationFactory
但是,您不能使用最新版本,因為:
-
突破性的變化(參見LOG4J2-1547)中的
log4j-core
使2.7.0版和更高版本與Spring Boot 1.2.x、不兼容aseries of security vulnerabilities將您的選擇進一步限制為2.3.2版。
使用2.3.2版需要將log4j.properties
文件轉換為Log4j 2.x格式(可以使用轉換器from this question):
<?xml version="1.0"?>
<Configuration name="Log4j1">
<Appenders>
<RollingFile name="file" fileName="/var/log/access.log"
filePattern="/var/log/access.log.%i">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
<Policies>
<SizeBasedTriggeringPolicy size="5MB" />
</Policies>
<DefaultRolloverStrategy max="10" />
</RollingFile>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="file" />
</Root>
</Loggers>
</Configuration>
并設置系統屬性:
logging.config=/path/to/the/file/above.xml
備注:Spring Boot提供了一系列啟動器,可以拉取正確的依賴項。要使用Log4j 2.x,只需排除標準spring-boot-starter-logging
,包含spring-boot-starter-log4j2
即可。不需要顯式的Log4j依賴項(如果在代碼中使用Log4j 1.x,則log4j-1.2-api
除外):
<properties>
<log4j2.version>2.3.2</log4j2.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
</dependencies>
這篇關于在Spring Boot Web應用程序中使用橋JAR從log4j 1.x遷移到log4j 2.x時無法生成日志的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,