一、新建工程
选择一些基础依赖 填写工程名称和项目路径
二、工程配置
修改文件编码格式 设置Java Compiler 修改maven配置文件路径
三、pom.xml的web依赖中排除掉logging依赖,并且引入log4j2依赖
<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>
四、springboot配置
logging:
config: classpath
:log4j2.xml
五、log4j2配置文件
<configuration status="OFF" monitorInterval="30">
<Properties>
<Property name="pattern">%d{yyyy-MM-dd HH:mm:ss,SSS}|%p|%c|%L|%m%n
</Property>
<Property name="logPath">logs
</Property>
</Properties>
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="${pattern}"/>
</Console>
<RollingRandomAccessFile name="debug"
immediateFlush="false"
fileName="${logPath}/debug.log"
filePattern="${logPath}/debug-%d{yyyy-MM-dd}.log">
<Filters>
<ThresholdFilter level="DEBUG"/>
<ThresholdFilter level="INFO" onMatch="DENY" onMismatch="NEUTRAL"/>
</Filters>
<PatternLayout pattern="${pattern}"/>
<Policies>
<TimeBasedTriggeringPolicy modulate="true" interval="1"/>
</Policies>
<DefaultRolloverStrategy max="15"/>
</RollingRandomAccessFile>
<RollingRandomAccessFile name="info"
immediateFlush="false"
fileName="${logPath}/info.log"
filePattern="${logPath}/info-%d{yyyy-MM-dd}.log">
<Filters>
<ThresholdFilter level="INFO"/>
<ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL"/>
</Filters>
<PatternLayout pattern="${pattern}"/>
<Policies>
<TimeBasedTriggeringPolicy modulate="true" interval="1"/>
</Policies>
<DefaultRolloverStrategy max="60"/>
</RollingRandomAccessFile>
<RollingRandomAccessFile name="warn"
immediateFlush="false"
fileName="${logPath}/warn.log"
filePattern="${logPath}/warn-%d{yyyy-MM-dd}.log">
<Filters>
<ThresholdFilter level="WARN"/>
<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/>
</Filters>
<PatternLayout pattern="${pattern}"/>
<Policies>
<TimeBasedTriggeringPolicy modulate="true" interval="1"/>
</Policies>
<DefaultRolloverStrategy max="60"/>
</RollingRandomAccessFile>
<RollingRandomAccessFile name="error"
immediateFlush="false"
fileName="${logPath}/error.log"
filePattern="${logPath}/error-%d{yyyy-MM-dd}.log">
<PatternLayout pattern="${pattern}"/>
<Filters>
<ThresholdFilter level="ERROR"/>
</Filters>
<Policies>
<TimeBasedTriggeringPolicy modulate="true" interval="1"/>
</Policies>
<DefaultRolloverStrategy max="60"/>
</RollingRandomAccessFile>
<RollingRandomAccessFile name="task_suc"
immediateFlush="false"
fileName="${logPath}/task_suc.log"
filePattern="${logPath}/task_suc-%d{yyyy-MM-dd}.log">
<PatternLayout pattern="${pattern}"/>
<Policies>
<TimeBasedTriggeringPolicy modulate="true" interval="1"/>
</Policies>
<DefaultRolloverStrategy max="90"/>
</RollingRandomAccessFile>
<RollingRandomAccessFile name="task_fail"
immediateFlush="false"
fileName="${logPath}/task_fail.log"
filePattern="${logPath}/task_fail-%d{yyyy-MM-dd}.log">
<PatternLayout pattern="${pattern}"/>
<Policies>
<TimeBasedTriggeringPolicy modulate="true" interval="1"/>
</Policies>
<DefaultRolloverStrategy max="90"/>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<Logger name="com.example.log4j2demo" level="DEBUG" additivity="true">
<AppenderRef ref="debug"/>
<AppenderRef ref="info"/>
<AppenderRef ref="warn"/>
<AppenderRef ref="error"/>
</Logger>
<Logger name="org.springframework" level="ERROR"/>
<Logger name="org.apache" level="ERROR"/>
<Logger name="task_suc" additivity="true">
<AppenderRef ref="task_suc"/>
</Logger>
<Logger name="task_fail" additivity="true">
<AppenderRef ref="task_fail"/>
</Logger>
<Root level="DEBUG">
<AppenderRef ref="console"/>
</Root>
</Loggers>
</configuration>
注:
1、configuration 标签
<configuration status="OFF" monitorInterval="30">
2、Console标签
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="${pattern}"/>
</Console>
3、日志输出格式
<Property name="pattern">%d{yyyy-MM-dd HH:mm:ss,SSS}|%p|%c|%L|%m%n
</Property>
4、RollingRandomAccessFile 标签
<RollingRandomAccessFile name="debug"
immediateFlush="false"
fileName="${logPath}/debug.log"
filePattern="${logPath}/debug-%d{yyyy-MM-dd}.log">
<Filters>
<ThresholdFilter level="DEBUG"/>
<ThresholdFilter level="INFO" onMatch="DENY" onMismatch="NEUTRAL"/>
</Filters>
<PatternLayout pattern="${pattern}"/>
<Policies>
<TimeBasedTriggeringPolicy modulate="true" interval="1"/>
</Policies>
<DefaultRolloverStrategy max="15"/>
</RollingRandomAccessFile>
RollingRandomAccessFile 为异步日志,RollingFile 为同步日志。
5、Logger 标签
<Logger name="com.example.log4j2demo" level="DEBUG" additivity="true">
<AppenderRef ref="debug"/>
<AppenderRef ref="info"/>
<AppenderRef ref="warn"/>
<AppenderRef ref="error"/>
</Logger>
六、使用示例
@Slf4j
@SpringBootTest
class Log4j2DemoApplicationTests {
private static final Logger sucLog
= LoggerFactory
.getLogger("task_suc");
private static final Logger failLog
= LoggerFactory
.getLogger("task_fail");
private static final Logger sysLog
= LoggerFactory
.getLogger(Log4j2DemoApplicationTests
.class);
@Test
void log4j2() {
sucLog
.debug("task_suc debug");
sucLog
.info("task_suc info");
sucLog
.warn("task_suc warn");
sucLog
.error("task_suc error");
failLog
.debug("task_fail debug");
failLog
.info("task_fail info");
failLog
.warn("task_fail warn");
failLog
.error("task_fail error");
log
.debug("normal debug");
log
.info("normal info");
log
.warn("normal warn");
log
.error("normal error");
sysLog
.debug("sys debug");
sysLog
.info("sys info");
sysLog
.warn("sys warn");
sysLog
.error("sys error");
}
}
控制台输出结果:
日志文件: