Spring Boot 默认使用 Logback,但相比较而言,Log4j2 在性能上面会更好。
添加依赖:
除了添加log4j2的依赖,还要排除Springboot默认依赖的logback依赖
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
然后在src/main/resources
中添加log4j2.xml
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 共有8个级别,按照从低到高为:ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF -->
<!-- status指定log4j本身的打印日志的级别 -->
<Configuration status="WARN">
<Properties>
<Property name="base.package">com.example.demo</Property>
<Property name="log.level">${sys:log4j.log.level:-DEBUG}</Property>
<Property name="logfile.path">${sys:log4j.logfile.path:-/data/logs/access}/@artifactId@</Property>
<Property name="logfile.arch.path">${sys:log4j.logfile.path:-/data/logs/access}/@artifactId@/${date:yyyy-MM}</Property>
<Property name="logfile.arch.suffix">%d{yyyy-MM-dd}-%i.log.gz</Property>
<Property name="logOut.pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight{%5p} ${sys:PID} --- [%15.15t] %-40.40c{1.} : %m%n</Property>
<Property name="logfile.pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [%t] <%c> : %m%n</Property>
<Property name="timestamp.pattern">%d{yyyy-MM-dd HH:mm:ss.SSS}{GMT+0}</Property>
</Properties>
<Appenders>
<!-- 输出到控制台 -->
<Console name="STDOUT" target="SYSTEM_OUT">
<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="ACCEPT" />
<PatternLayout pattern="${logOut.pattern}"/>
</Console>
<Console name="STDERR" target="SYSTEM_ERR">
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY" />
<PatternLayout pattern="${logOut.pattern}"/>
</Console>
<!-- 输出到日志文件 -->
<RollingRandomAccessFile name="SERVICE_LOG_FILE" fileName="${logfile.path}/service.log"
filePattern="${logfile.arch.path}/service-${logfile.arch.suffix}">
<PatternLayout pattern="${logfile.pattern}"/>
<Policies>
<!-- 基于时间的滚动策略(interval属性). The default value is 1. -->
<TimeBasedTriggeringPolicy />
<!-- 基于文件大小的滚动策略(size属性):SizeBasedTriggeringPolicy -->
</Policies>
<!-- 日志删除策略 -->
<!-- 用来指定同一个文件夹下最多有几个日志文件时开始删除最旧的,创建新的(通过max属性). The default value is 7. -->
<DefaultRolloverStrategy>
<!-- maxDepth:要访问的目录的最大级别数 -->
<Delete basePath="${logfile.path}" maxDepth="2">
<!-- 按通配符匹配文件 -->
<IfFileName glob="*/service-*.log.gz" />
<!-- 删除180天之前的 -->
<IfLastModified age="180d" />
</Delete>
</DefaultRolloverStrategy>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<!-- Root Logger -->
<Root level="INFO">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="STDERR"/>
<AppenderRef ref="SERVICE_LOG_FILE" />
</Root>
<!-- additivity="false"只在自定义的Appender中进行输出 -->
<!-- Base Logger -->
<Logger name="${base.package}" level="${log.level}"></Logger>
</Loggers>
</Configuration>
具体使用就不介绍,通常是通过lombok
的@Slf4j