log4j升级log4j2遇到的问题及解决⽅式
⽬录
log4j升级log4j2的问题
⼀、导⼊包
⼆、在src/main/resources下新建⼀个l⽂件
升级log4j2遇到的那些坑
log4j升级log4j2的问题
⼀、导⼊包
<!-- 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>
注意:可能有些包需要依赖log4j,但是升级后只有log4j2了,此时会报不到类的错,如:
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
那么如果这些包没有使⽤slf4接⼝,⽽是写死了必须依赖log4j的话,如果实在不想导⼊log4j的包,那么只能能代替这个包的⼯具包使⽤了
如:
<!-- 使⽤该报去掉log4j,后会报上述错误,所以就把这个包换了 -->
<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下新建⼀个l⽂件
配置如下
<?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>
<!-- 控制台⽇志打印 -->
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${PATTERN}"/>
</Console>
<!-- ⽣产环境⽇志打印 -->
<!-- fileName表⽰创建的⽇志⽂件名,filePattern表⽰如果⽇志策略按照⽇期与⼤⼩做限定的话,如果⼤⼩超过50m就会按该格式将之前的的⽇志⽂件重命名,并重新创建新的fileName。同理,如果⽇期超过限定的时间,也会进⾏同样的操作,下⽂定义⼀天建⼀个,  <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的默认输出级别和⽅式 -->
<loggers>
<root level="error">
<appender-ref ref="Console" />
<appender-ref ref="ProdFile" />
</root>
</loggers>
</configuration>
升级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>
<!--这块⽐较关键,Spring原⽣应⽤中需要⽤到commons-logging,需要把它exclude掉,另外引⼊jcl-over-slf4j--> <!--你会发现jcl-over-slf4j 只是改写了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>
<!-- 如果代码中依赖的jar包⽤到了log4j1.2.X,当⼯程中将log4j1.2.X排除掉了之后,项⽬启动时会报错不成功的 --> <!-- 把下⾯这个jar引⼊进来就OK了 -->
<dependency>
log4j2不打印日志<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。