spring boot 整合log4j2
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>
4 还可以根据不同业务场景指定不同日志记录到不同日志文件,比如需要跳过不处理的数据,比如订单日志等。需要在记录日志时添加标志信息,日志配置文件中配置标志信息对应的日志文件即可
(1) java中
log.info(MarkerFactory.getMarker("SKIP_DATA"), "此处记录详细日志信息");
(2) log4j2.xml
<RollingFile name="RollingFileSkipData" fileName="${log_path}/report_skip.log" filePattern="${log_path}/history/report_skip-%d{yyyy-MM-dd}-%i.log">
<Filters>
<MarkerFilter marker="SKIP_DATA" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<PatternLayout charset="utf-8"
pattern="[%d{yyyy-MM-dd HH:mm:ss:SSS}] [%p] - %l - %m%n" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
</RollingFile>
...
<AppenderRef ref="RollingFileSkipData"/>