Version: Next
日志框架——Log4j2
导入依赖
info
要使用Log4j2,需要将SpringBoot的默认日志某块移除,同时导入Log4j2的依赖
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
编写Log4j配置文件
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout charset="UTF-8" pattern="[%-5p] %d %c - %m%n" />
</Console>
<RollingFile name="runtimeFile" fileName="./logs/boot-launch.log" filePattern="./logs/boot-launch-%d{yyyy-MM-dd}.log"
append="true">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS Z}\t%level\t%class\t%line\t%thread\t%msg%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
</Policies>
<!-- 此行以下为自动清理日志的配置 -->
<DefaultRolloverStrategy>
<Delete basePath="./logs">
<!-- glob 项为需要自动清理日志的pattern -->
<IfFileName glob="*.log"/>
<!-- 30d 表示自动清理掉30天以前的日志文件 -->
<IfLastModified age="30d"/>
</Delete>
</DefaultRolloverStrategy>
<!-- 此行以上为自动清理日志的配置 -->
</RollingFile>
</Appenders>
<Loggers>
<root level="info">
<AppenderRef ref="CONSOLE" />
<AppenderRef ref="runtimeFile" />
</root>
</Loggers>
</configuration>
在SpringBoot配置中开启
application.xml
mybatis:
mapper-locations: classpath:mapper/*.xml
configuration:
map-underscore-to-camel-case: true #驼峰映射
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #调试打印SQL
logging:
config: classpath:log4j2.xml