SpringBoot集成log4j,解决log4j.properties不生效问题

Spring Boot集成log4j其实比较简单,maven的话,在xml中增加log4j依赖就行

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>

然后配置log4j.properties(每一行是啥意思,自行百度)

log4j.rootLogger=info,stdout, infofile
######################################################################
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
######################################################################
log4j.appender.infofile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.infofile.File=/log/infofile-log.log
log4j.appender.infofile.Append=true
log4j.appender.infofile.Threshold=info
log4j.appender.infofile.layout=org.apache.log4j.PatternLayout
log4j.appender.infofile.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
log4j.appender.infofile.encoding=GBK

但是、启动项目后,我发现并没有生成 /log/infofile-log.log 文件

网上查了一下,大概原因是jar包冲突,因为 spring-boot-starter-web、spring-boot-starter-data-jpa、spring-boot-starter-jdbc等下面有一些logback、log4j、slf4j等相关的包,我用 dependency:tree命令查到具体是那些jar,然后一一排除,这样再启动时,就会发现 infofile-log.log文件创建成功了

这种方法比较繁琐,但是可以更理解jar包依赖关系

 

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>

 --------------------------------------------------------------------------------------------------------------------------------------------------------------

看这里,更优雅一些: https://www.cnblogs.com/myfrank/p/14286815.html

posted @ 2020-09-25 15:23  袁骞骞  阅读(6825)  评论(0编辑  收藏  举报