log4j升級(jí)log4j2遇到的問(wèn)題及解決方式
log4j升級(jí)log4j2的問(wèn)題
一、導(dǎo)入包
<!-- log -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<!-- 日志橋接包, 適配log4j2 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.8.2</version>
</dependency>
注意:可能有些包需要依賴(lài)log4j,但是升級(jí)后只有l(wèi)og4j2了,此時(shí)會(huì)報(bào)找不到類(lèi)的錯(cuò),如:
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
那么如果這些包沒(méi)有使用slf4接口,而是寫(xiě)死了必須依賴(lài)log4j的話(huà),如果實(shí)在不想導(dǎo)入log4j的包,那么只能找能代替這個(gè)包的工具包使用了
如:
<!-- 使用該報(bào)去掉log4j,后會(huì)報(bào)上述錯(cuò)誤,所以就把這個(gè)包換了 --> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> <exclusions> <exclusion> <artifactId>log4j</artifactId> <groupId>log4j</groupId> </exclusion> </exclusions> </dependency>
替換為如下的包,就可以了:
<dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> <exclusions> <exclusion> <artifactId>log4j</artifactId> <groupId>log4j</groupId> </exclusion> <exclusion> <artifactId>slf4j-log4j12</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency>
二、在src/main/resources下新建一個(gè)log4j2.xml文件
配置如下
<?xml version="1.0" encoding="UTF-8" ?>
<configuration status="ERROR">
<Properties>
<Property name="PATTERN" value="[%d{HH:mm:ss.SSS} %-5level] [%t] %c{3} - %msg%n" />
<property name="FILE_PATH" value="/mySoft/logs" />
<property name="FILE_SIZE" value="50 MB" />
</Properties>
<Appenders>
<!-- 控制臺(tái)日志打印 -->
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${PATTERN}"/>
</Console>
<!-- 生產(chǎn)環(huán)境日志打印 -->
<!-- fileName表示創(chuàng)建的日志文件名,filePattern表示如果日志策略按照日期與大小做限定的話(huà),如果大小超過(guò)50m就會(huì)按該格式將之前的的日志文件重命名,并重新創(chuàng)建新的fileName。同理,如果日期超過(guò)限定的時(shí)間,也會(huì)進(jìn)行同樣的操作,下文定義一天建一個(gè),如果想一分鐘建一個(gè)則filePattern="${FILE_PATH}/demo-%d{yyyy-MM-dd HH-mm}-%i.log" 如此定義-->
<RollingFile name="ProdFile"
fileName="${FILE_PATH}/demo.log"
filePattern="${FILE_PATH}/demo-%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="${PATTERN}" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="${FILE_SIZE}"/>
</Policies>
<Filters>
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY" />
</Filters>
</RollingFile>
</Appenders>
<!-- root部分定義了log4j2的默認(rèn)輸出級(jí)別和方式 -->
<loggers>
<root level="error">
<appender-ref ref="Console" />
<appender-ref ref="ProdFile" />
</root>
</loggers>
</configuration>
升級(jí)log4j2遇到的那些坑
<slf4j.version>1.7.13</slf4j.version>
<log4j2.version>2.3</log4j2.version>
<disruptor.version>3.3.2</disruptor.version>
<spring.version>3.2.4.RELEASE</spring.version>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>${disruptor.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2.version}</version>
</dependency>
<!--這塊比較關(guān)鍵,Spring原生應(yīng)用中需要用到commons-logging,需要把它exclude掉,另外引入jcl-over-slf4j-->
<!--你會(huì)發(fā)現(xiàn)jcl-over-slf4j 只是改寫(xiě)了commons-logging 去兼容Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
<scope>runtime</scope>
</dependency>
<!-- 如果代碼中依賴(lài)的jar包用到了log4j1.2.X,當(dāng)工程中將log4j1.2.X排除掉了之后,項(xiàng)目啟動(dòng)時(shí)會(huì)報(bào)錯(cuò)不成功的 -->
<!-- 把下面這個(gè)jar引入進(jìn)來(lái)就OK了 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
通過(guò)Spring Boot + Mybatis + Redis快速搭建現(xiàn)代化Web項(xiàng)目
本篇文章介紹了如何通過(guò)Spring Boot、Mybatis以及Redis快速搭建一個(gè)現(xiàn)代化的Web項(xiàng)目,并且同時(shí)介紹了如何在Spring Boot下優(yōu)雅地書(shū)寫(xiě)單元測(cè)試來(lái)保證我們的代碼質(zhì)量。具體內(nèi)容詳情大家通過(guò)本文學(xué)習(xí)下吧2017-12-12
java中用String.Join美化代碼的實(shí)例講解
在本篇文章里小編給大家整理的是一篇關(guān)于java中用String.Join美化代碼的實(shí)例講解內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2020-12-12
SpringBoot單機(jī)限流的實(shí)現(xiàn)
在系統(tǒng)運(yùn)維中, 有時(shí)候?yàn)榱吮苊庥脩?hù)的惡意刷接口, 會(huì)加入一定規(guī)則的限流,本文主要介紹了SpringBoot單機(jī)限流的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08
springboot2.x使用Jsoup防XSS攻擊的實(shí)現(xiàn)
這篇文章主要介紹了springboot2.x使用Jsoup防XSS攻擊的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
java控制臺(tái)輸出圖書(shū)館管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java控制臺(tái)輸出圖書(shū)館管理系統(tǒng),只用java代碼不用數(shù)據(jù)庫(kù)和GUI等,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
springboot啟動(dòng)前執(zhí)行方法的四種方式總結(jié)
這篇文章主要給大家介紹了關(guān)于springboot啟動(dòng)前執(zhí)行方法的四種方式,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01

