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下新建一个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>
<!-- 控制台日志打印 -->
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${PATTERN}"/>
</Console>
<!-- 生产环境日志打印 -->
<!-- fileName表示创建的日志文件名,filePattern表示如果日志策略按照日期与大小做限定的话,如果大小超过50m就会按该格式将之前的的日志文件重命名,并重新创建新的fileName。同理,如果日期超过限定的时间,也会进行同样的操作,下文定义一天建一个,如果想一分钟建一个则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的默认输出级别和方式 -->
<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>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。