先在maven里把spring-boot-starter里的spring-boot-starter-logging依赖去掉
<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>
添加log4j2的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
在application.yml里配置日志设置文件
logging:
config: classpath:log4j2.properties
log4j2.properties内容
status = warn
name = MyApp
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %-d{yyyy-MM-dd HH:mm:ss} [ %-5p ] %m (%c:%L)%n
appender.rolling.type = File
appender.rolling.name = log
appender.rolling.append = true
appender.rolling.fileName = d:\\test1.log
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %-d{yyyy-MM-dd HH:mm:ss} [ %-5p ] %m (%c:%L)%n
rootLogger.level = debug
rootLogger.appenderRef.stdout.ref = STDOUT
rootLogger.appenderRef.log.ref = log
本文介绍如何在Spring Boot项目中排除默认的日志依赖并引入log4j2进行日志记录。通过修改Maven配置文件去除spring-boot-starter-logging依赖,并添加spring-boot-starter-log4j2依赖。同时详细展示了如何在application.yml中指定log4j2配置文件路径及log4j2.properties文件的具体配置。
2829

被折叠的 条评论
为什么被折叠?



