1. 删除 springboot 中默认集成的 logback
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
<version>${mybatis-spring-boot-starter.version}</version>
</dependency>
2. 添加 log4j2 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
3. log4j2.xml 配置
<?xml version="1.0" encoding="UTF-8"?>
<!-- status 负责打印日记系统的 WARN 级别以及以上的日记 -->
<Configuration status="INFO">
<Properties>
<Property name="logFilePath">logs</Property>
<Property name="logFileName">XXdir</Property>
<Property name="logPattern">%d [%t] %-5level %logger{36} - %m%n%throwable</Property>
</Properties>
<Appenders>
<!--很直白,Console指定了结果输出到控制台-->
<Console name="ConsolePrint" target="SYSTEM_OUT">
<PatternLayout pattern="${logPattern}"/>
</Console>
<!--<File>输出结果到指定文件</File>-->
<!--<RollingFile>同样输出结果到指定文件,但是使用buffer,速度会快点</RollingFile>-->
<!--filePattern:表示当日志到达指定的大小或者时间,产生新日志时,旧日志的命名路径。-->
<!--PatternLayout:和log4j一样,指定输出日志的格式,append表示是否追加内容,值默认为true-->
<RollingFile name="RollingFileDebug" fileName="${logFilePath}/${logFileName}-debug.csv"
filePattern="${logFilePath}/$${date:yyyy-MM}/${logFileName}-%d{yyyy-MM-dd}_%i-debug.csv.gz">
<PatternLayout pattern="${logPattern}"/>
<!--注意,如果有多个ThresholdFilter,那么Filters标签是必须的-->
<Filters>
<!--首先需要过滤不符合的日志级别,把不需要的首先DENY掉,然后在ACCEPT需要的日志级别,次序不能颠倒-->
<!--INFO及以上级别拒绝输出-->
<ThresholdFilter level="INFO" onMatch="DENY" onMismatch="NEUTRAL"/>
<!--只输出DEBUG级别信息-->
<ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<Policies>
<OnStartupTriggeringPolicy />
<!--时间策略,每隔24小时产生新的日志文件-->
<TimeBasedTriggeringPolicy/>
<!--大小策略,每到300M时产生新的日志文件-->
<SizeBasedTriggeringPolicy size="300MB"/>
</Policies>
<!-- 计数器的最大值。一旦达到这个值,旧的档案将在随后的rollover中被删除。 -->
<DefaultRolloverStrategy max="200">
<!-- Nested conditions: the inner condition is only evaluated on files for which the outer conditions are true. -->
<!-- basePath必参。从哪里扫描要删除的文件的基本路径 要访问的目录的最大级别数。值为0表示仅访问起始文件(基本路径本身),除非被安全管理者拒绝。Integer.MAX_VALUE的值表示应该访问所有级别。默认为1,意思是指定基本目录中的文件。-->
<Delete basePath="${logFilePath}" maxDepth="2">
<IfFileName glob="*/kpc-*-debug.log.gz">
<!-- debug日记只保留10天的 -->
<IfLastModified age="10d">
<IfAny>
<IfAccumulatedFileSize exceeds="30MB" />
<IfAccumulatedFileCount exceeds="20" />
</IfAny>
</IfLastModified>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<RollingFile name="RollingFileInfo" fileName="${logFilePath}/${logFileName}-info.csv"
filePattern="${logFilePath}/$${date:yyyy-MM}/${logFileName}-%d{yyyy-MM-dd}_%i-info.csv.gz">
<PatternLayout pattern="${logPattern}"/>
<Filters>
<!--首先需要过滤不符合的日志级别,把不需要的首先DENY掉,然后在ACCEPT需要的日志级别,次序不能颠倒-->
<!--只输出DEBUG级别以上的信息-->
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
<ThresholdFilter level="FATAL" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<Policies>
<OnStartupTriggeringPolicy />
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="300MB"/>
</Policies>
<DefaultRolloverStrategy max="200">
<!-- Nested conditions: the inner condition is only evaluated on files for which the outer conditions are true. -->
<Delete basePath="${logFilePath}" maxDepth="2">
<IfFileName glob="*/kpc-*-info.log.gz">
<!-- debug日记只保留1个月天的 -->
<IfLastModified age="30d">
<IfAny>
<IfAccumulatedFileSize exceeds="30 MB" />
<IfAccumulatedFileCount exceeds="20" />
</IfAny>
</IfLastModified>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<Async name="ASYNC">
<AppenderRef level="info" ref="ConsolePrint" />
<Appender-ref ref="RollingFileDebug"/>
<Appender-ref ref="RollingFileInfo"/>
</Async>
</Appenders>
<Loggers>
<!-- additivity=false:表示只用当前logger的appender-ref。 -->
<Logger name="cn.zhangguihong" level="debug" additivity="false">
<AppenderRef ref="ASYNC" />
</Logger>
<Root level="info" includeLocation="true">
<AppenderRef ref="ASYNC" />
</Root>
</Loggers>
</Configuration>
4. application.properties 中添加 logging.config , 当程序中只有一个log4j2.xml的时候可以不添加 ,
项目中可以按阶段配置多个 xml 文件, 例如: log4j2-dev.xml
logging.config=classpath:log4j2.xml