Log4j2扩展
特定配置文件
<SpringProfile name="staging">
<!-- 当“staging”配置文件处于活动状态时启用的配置 -->
</SpringProfile>
<SpringProfile name="dev | staging">
<!-- 当“dev”或“staging”配置文件处于活动状态时启用的配置 -->
</SpringProfile>
<SpringProfile name="!production">
<!-- 当“production”配置文件未处于活动状态时启用的配置 -->
</SpringProfile>
环境属性查找
<Properties>
<Property name="applicationName">${spring:spring.application.name}</Property>
</Properties>
maven
<dependencies>
<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>
</dependencies>
log4j2.xml
读取spring的配置属性需要使用
${spring:xxxxx}
<?xml version="1.0" encoding="UTF-8"?>
<Configuration name="Config" status="INFO" monitorInterval="5">
<Properties>
<Property name="serviceName">${spring:service.name}</Property>
</Properties>
<Appenders>
<SpringProfile name="dev | staging">
<Console name="Out">
<PatternLayout pattern="%m%n"/>
</Console>
</SpringProfile>
<SpringProfile name="prod">
<Console name="Out" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - ${serviceName} - %msg%n"/>
</Console>
</SpringProfile>
</Appenders>
<Loggers>
<Logger name="org.apache.test" level="INFO" additivity="false">
<AppenderRef ref="Out"/>
</Logger>
<Root level="INFO">
<AppenderRef ref="Out"/>
</Root>
</Loggers>
</Configuration>