springboot日志配置文件log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="0">
<!-- 配置公共的有可能调整的变量 -->
<Properties>
<!--应用名称-->
<property name="APP.NAME">springboot-helloword</property>
<!-- 日志级别 -->
<Property name="LOG.LEVEL">info</Property>
<!-- 日志文件根目录 -->
<Property name="LOG.HOME">/yk/logs/${APP.NAME}/</Property>
<!-- 日志输出表达式 -->
<!-- <property name="LOG.PATTERN">%-d{yyyy-MM-dd HH:mm:ss.SSS}[ %t:%r ] - [%-5p] %c-%M:%L - %X{PFTID} - %m%n%throwable{full}</property>-->
<Property name="LOG.PATTERN">[%p]%d{yyyy-MM-dd HH:mm:ss.SSS}[%X{trace_id}][%t][%c#%M:%L]%m %n</Property>
<!-- Buffer单位为字节,默认是8K,IO BLOCK大小默认也是8K。即当日志内容达到8k时,才会将日志输出到日志输出目的地(使用Async logger输出日志,buffersize在相应日志频率下,简单来说越大越好) -->
<Property name="RollingRandomAccessFile.bufferSize">8192</Property>
<!-- 是否追加 -->
<Property name="RollingRandomAccessFile.append">true</Property>
<!-- 日志按日期封存策略(每天封存一次) -->
<Property name="TimeBasedTriggeringPolicy.interval">1</Property>
<!-- 是否打印方法名和行号(开启对性能有影响,可以从异常堆栈找方法名和行号) -->
<Property name="Logger.includeLocation">true</Property>
<!-- 开启会将日志打到具体文件和控制台,生产环境建议只打到文件即可 -->
<Property name="Logger.additivity">false</Property>
</Properties>
<!-- 输出控制 -->
<Appenders>
<!-- 控制台输出 -->
<Console name="Console" target="SYSTEM_OUT">
<!-- 控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
<!-- 输出日志的格式(控制台简单输出即可) -->
<!-- <PatternLayout pattern="[%p]%d{HH:mm:ss} %m [%t][%c{1}#%M:%L]%n" />-->
<PatternLayout pattern="${LOG.PATTERN}"/>
</Console>
<!-- debug -->
<RollingRandomAccessFile name="debug" fileName="${LOG.HOME}/debug/debug.log"
filePattern="${LOG.HOME}/debug/debug-%d{yyyyMMdd}-%i.log"
bufferSize="${RollingRandomAccessFile.bufferSize}" immediateFlush="false"
append="${RollingRandomAccessFile.append}">
<Filters>
<ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="${LOG.PATTERN}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="${TimeBasedTriggeringPolicy.interval}"
modulate="true"/><!-- 按天封存 -->
<SizeBasedTriggeringPolicy size="100MB"/><!-- 当文件体积大于size指定的值时,触发Rolling -->
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${LOG.HOME}/debug" maxDepth="1">
<IfFileName glob="debug-*.log">
<IfAny>
<IfAccumulatedFileCount exceeds="10"/>
</IfAny>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
</RollingRandomAccessFile>
<!-- info -->
<RollingRandomAccessFile name="info" fileName="${LOG.HOME}/info/info.log"
filePattern="${LOG.HOME}/info/info-%d{yyyyMMdd}-%i.log"
bufferSize="${RollingRandomAccessFile.bufferSize}" immediateFlush="false"
append="${RollingRandomAccessFile.append}">
<Filters>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="${LOG.PATTERN}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="${TimeBasedTriggeringPolicy.interval}"
modulate="true"/><!-- 按天封存 -->
<SizeBasedTriggeringPolicy size="500MB"/><!-- 当文件体积大于size指定的值时,触发Rolling -->
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${LOG.HOME}/info" maxDepth="1">
<IfFileName glob="info-*.log">
<IfAny>
<IfAccumulatedFileCount exceeds="40"/>
</IfAny>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
</RollingRandomAccessFile>
<!-- warn -->
<RollingRandomAccessFile name="warn" fileName="${LOG.HOME}/warn/warn.log"
filePattern="${LOG.HOME}/warn/warn-%d{yyyyMMdd}-%i.log"
bufferSize="${RollingRandomAccessFile.bufferSize}" immediateFlush="false"
append="${RollingRandomAccessFile.append}">
<Filters>
<ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="${LOG.PATTERN}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="${TimeBasedTriggeringPolicy.interval}"
modulate="true"/><!-- 按天封存 -->
<SizeBasedTriggeringPolicy size="100MB"/><!-- 当文件体积大于size指定的值时,触发Rolling -->
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${LOG.HOME}/warn" maxDepth="1">
<IfFileName glob="warn-*.log">
<IfAny>
<IfAccumulatedFileCount exceeds="40"/>
</IfAny>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
</RollingRandomAccessFile>
<!-- error -->
<RollingRandomAccessFile name="error" fileName="${LOG.HOME}/error/error.log"
filePattern="${LOG.HOME}/error/error-%d{yyyyMMdd}-%i.log"
bufferSize="${RollingRandomAccessFile.bufferSize}" immediateFlush="false"
append="${RollingRandomAccessFile.append}">
<Filters>
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout pattern="${LOG.PATTERN}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="${TimeBasedTriggeringPolicy.interval}"
modulate="true"/><!-- 按天封存 -->
<SizeBasedTriggeringPolicy size="100MB"/><!-- 当文件体积大于size指定的值时,触发Rolling -->
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${LOG.HOME}/error" maxDepth="1">
<IfFileName glob="error-*.log">
<IfAny>
<IfAccumulatedFileCount exceeds="40"/>
</IfAny>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
</RollingRandomAccessFile>
</Appenders>
<!-- 异步和非异步混合输出模式 -->
<Loggers>
<!-- 只输出框架或三方包里的异常日志,此处视情况可以补充调整 -->
<!-- <AsyncLogger name="com.mysql" level="ERROR"/>-->
<!-- <AsyncLogger name="com.zaxxer.hikari.pool.HikariPool" level="ERROR"/>-->
<!-- <AsyncLogger name="org.springframework" level="ERROR" />-->
<!-- <AsyncLogger name="com.ibatis" level="ERROR" />-->
<!-- <AsyncLogger name="org.mybatis.spring" level="WARN" />-->
<!-- <AsyncLogger name="org.apache.ibatis" level="ERROR"/>-->
<!-- <AsyncLogger name="org.apache" level="ERROR" />-->
<!-- <AsyncLogger name="org.apache.commons" level="ERROR" />-->
<!-- <AsyncLogger name="io.netty" level="ERROR" />-->
<!-- <AsyncLogger name="com.alibaba" level="ERROR" />-->
<!-- /只输出框架或三方包里的异常日志,此处视情况可以补充调整 -->
<!-- 配置MyBatis日志级别 -->
<!-- <Logger name="org.apache.ibatis" level="debug" additivity="${Logger.additivity}">-->
<!-- <AppenderRef ref="Console"/>-->
<!-- </Logger>
<!- 说明:业务日志。additivity="false"开发环境可以设置为true,便于日志打到控制台,生产环境设置为false -->
<AsyncLogger name="com.yk" level="${LOG.LEVEL}" includeLocation="${Logger.includeLocation}"
additivity="${Logger.additivity}">
<AppenderRef ref="Console"/>
<AppenderRef ref="debug"/>
<AppenderRef ref="info"/>
<AppenderRef ref="warn"/>
<AppenderRef ref="error"/>
</AsyncLogger>
<!-- 业务日志已使用AsyncLogger做异步输出,所以跟节点可以不用AsyncRoot 控制台日志输出-->
<Root level="${LOG.LEVEL}" includeLocation="${Logger.includeLocation}">
<!-- <AppenderRef ref="Console"/>-->
<AppenderRef ref="debug"/>
<AppenderRef ref="info"/>
<AppenderRef ref="warn"/>
<AppenderRef ref="error"/>
</Root>
</Loggers>
</Configuration>
ymal配置
mybatis:
mapper-locations: classpath*:mapper/*.xml
type-aliases-package: com.yk.domain
configuration:
map-underscore-to-camel-case: false
log-impl: org.apache.ibatis.logging.log4j2.Log4j2Impl
logging:
config: classpath:log4j2.xml
pom.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yk</groupId>
<artifactId>springboot-hellword</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-hellword</name>
<description>springboot-hellword</description>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.boot.version>3.2.3</spring.boot.version>
</properties>
<dependencies>
<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-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.13.0</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<!-- MySQL 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
<!-- <scope>runtime</scope>-->
</dependency>
<!-- 引入这个依赖是为了能够使用异步打印 -->
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.3.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>