spring boot 整合log4j2
阿新 • • 發佈:2021-08-05
1 pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>logback-core</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
<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</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-logging</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency> <!-- 引入log4j2依賴 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
2 application.properties 配置log4j2.xml
logging.config=classpath:log4j2.xml
3 log4j2.xml配置資訊
<Configuration status="INFO" monitorInterval="30">
<Properties>
<!-- 輸出路徑 -->
<Property name="pattern">[%d][%-5p][%t] %m (%F:%L)%n</Property>
<Property name="log_path">./logs</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="${pattern}"/>
</Console>
<RollingFile name="debug" fileName="${log_path}/log_debug.log"
filePattern="${log_path}/history/log_debug_%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout charset="utf-8" pattern="${pattern}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" />
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
<DefaultRolloverStrategy max="30" />
</RollingFile>
<RollingFile name="info" fileName="${log_path}/log_info.log"
filePattern="${log_path}/history/log_info_%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout charset="utf-8" pattern="${pattern}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" />
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
<DefaultRolloverStrategy max="30" />
</RollingFile>
<RollingFile name="error" fileName="${log_path}/log_error.log"
filePattern="${log_path}/history/log_error_%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY" />
</Filters>
<PatternLayout charset="utf-8" pattern="${pattern}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" />
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
<DefaultRolloverStrategy max="30" />
</RollingFile>
</Appenders>
<!--切換輸出級別-->
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
<AppenderRef ref="debug"/>
<AppenderRef ref="info"/>
<AppenderRef ref="error"/>
</Root>
</Loggers>
</Configuration>