SpringBoot -- 使用log4j2 实例
作者:互联网
前提:
创建springboot项目:SpringBoot -- idea创建简单的helloWorld项目_DGH2430284817的博客-CSDN博客
使用log4j2:
添加依赖
<dependencies>
<!-- 引入web模块 -->
<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>
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
配置文.yml 添加日志参数
# log4j 配置
logging:
config: classpath:log4j2.xml
level:
org.springfromework.web: info
添加日志配置文件log4j2.xml
主要看LOG_HOME 日志存放路径,PATTERN_LAYOUT 日志格式,ROLLFILE 日志文件信息
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<properties>
<property name="LOG_HOME">./logs/flinklog</property>
<property name="PATTERN_LAYOUT">%d{yyyy-MM-dd HH:mm:ss.SSS} [%-16.16X{traceId}] %X{tid} %-5p [%t] [%l] %c{1.} - %m%n</property>
</properties>
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="${PATTERN_LAYOUT}" />
</Console>
<RollingFile name="ROLLFILE" fileName="${LOG_HOME}/flink-test.log" filePattern="${LOG_HOME}/flink-test.%d{yyyy-MM-dd}.gz">
<PatternLayout>
<Pattern>${PATTERN_LAYOUT}</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy modulate="true" interval="1" />
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${LOG_HOME}/" maxDepth="1">
<IfFileName glob="asset-wallet*.gz" />
<IfLastModified age="7d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<RollingFile name="ERROR" fileName="${LOG_HOME}/flink-test-error.log" filePattern="${LOG_HOME}/flink-test-error.%d{yyyy-MM-dd}.gz">
<PatternLayout>
<Pattern>${PATTERN_LAYOUT}</Pattern>
</PatternLayout>
<Filters>
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<Policies>
<TimeBasedTriggeringPolicy modulate="true" interval="1" />
</Policies>
<DefaultRolloverStrategy>
<Delete basePath="${LOG_HOME}/" maxDepth="1">
<IfFileName glob="asset-wallet-error*.gz" />
<IfLastModified age="7d" />
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="CONSOLE" />
<AppenderRef ref="ROLLFILE" />
<AppenderRef ref="ERROR" />
</Root>
</Loggers>
</Configuration>
测试:
创建测试类,MyLogTest.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyLogTest {
private static final Logger log = LoggerFactory.getLogger(MyLogTest.class);
public static void main(String[] args) {
log.info("test-> info");
log.debug("test-> debug");
log.warn("test-> warn");
log.error("test-> error");
}
}
效果:
查看配置的日志目录,查看是否有对应日志文件
测试成功
标签:SpringBoot,--,boot,test,org,日志,log4j2,log 来源: https://blog.csdn.net/DGH2430284817/article/details/123071895