Background: Since the version beginning with spring boot 1.4 is going to be log4j2, the format supported is JSON and XML two formats, this practice mainly uses the XML format definition log description.
The development steps for spring boot 1.5.8.RELEASE to introduce log4j2 are as follows:
1. First remove the Spring-boot-starter-web and the spring-boot-starter-logging below the Spring-boot-starter package, The SPRING-BOOT-STARTER-LOG4J2 package is then introduced.
<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>
<!--https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-log4j2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>1.5.8.RELEASE</version>
</dependency>
Set the file resource location in 2.pom to modify the variables in the file through the properties in the Pom.
<finalName>${project.name}</finalName>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
Set Variable substitution properties
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<log4j2.level>debug</log4j2.level>
<log4j2.root.path>/opt/appstack/apache-tomcat/logs/${project.name}</log4j2.root.path>
<log4j2.error.path>/opt/appstack/apache-tomcat/logs/${project.name}-error</log4j2.error.path>
<log4j2.package.path>/opt/appstack/apache-tomcat/logs/${project.name}-kk</log4j2.package.path>
</properties>
3. Because spring boot has a protective effect on the configuration file Yml or properties, the variables in the Pom need to be added with a plug-in to replace the variables in the configuration file.
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--spring-boot to protect application.yml and application.properties, the default placeholder ${...} was modified. For @[email protected]>
<!--Yml and properties files for spring boot can be replaced with MAVEN variables using the ${} placeholder--
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>utf-8</encoding>
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
</pluginManagement>
4. This example uses the spring boot YML configuration to configure the Log4j2 configuration file in Application.yml
Logging
Config:classpath:log4j2.xml
If you want to configure different log4j2 configuration files for different environments, you can set the config value in the corresponding APPLICATION.YML
5. Configuring the Log4j2.xml configuration file
<?xml version= "1.0" encoding= "UTF-8"?>
<configuration status= "WARN" >
<!--global Parameters--
<Properties>
<property name= "pattern" >%d{yyyy-mm-dd hh:mm:ss,sss}%5p%c{1}:%l-%m%n</property>
</Properties>
<Loggers>
<root level= "DEBUG" >
<appenderref ref= "Console" ></AppenderRef>
<appenderref ref= "Rolling_file" ></AppenderRef>
</Root>
<!--only write Com.kk.springboot.demo to file--
<logger name= "Com.kk.springboot.demo" level= "${log4j2.level}" >
<appenderref ref= "File" ></AppenderRef>
</Logger>
</Loggers>
<Appenders>
<console name= "Console" target= "System_out" follow= "true" >
<!--console only outputs level and above information-
<thresholdfilter level= "INFO" onmatch= "ACCEPT" onmismatch= "DENY"/>
<PatternLayout>
<Pattern>${pattern}</Pattern>
</PatternLayout>
</Console>
<!--a appender from the same source can define multiple rollingfile, define log-per-day storage-
<rollingfile name= "Rolling_file"
Filename= "${log4j2.root.path}.log"
filepattern= "${log4j2.root.path}_%d{yyyy-mm-dd}.log" >
<thresholdfilter level= "INFO" onmatch= "ACCEPT" onmismatch= "DENY"/>
<PatternLayout>
<Pattern>${pattern}</Pattern>
</PatternLayout>
<Policies>
<timebasedtriggeringpolicy interval= "1"/>
<!--<sizebasedtriggeringpolicy size= "1 KB"/>-->
</Policies>
</RollingFile>
<file name= "File" filename= "${log4j2.package.path}.log" >
<!--<! – console only outputs level and above information (Onmatch), other direct rejections (Onmismatch) –>-->
<thresholdfilter level= "DEBUG" onmatch= "ACCEPT" onmismatch= "DENY"/>
<PatternLayout>
<Pattern>${pattern}</Pattern>
</PatternLayout>
</File>
</Appenders>
</configuration>
The Log4j2 log is used in the 6.Java program code.
private static Logger log = Loggerfactory.getlogger (Usercontroller.class);
Log.info ("Enter in Entityparam");
......
7. By changing the system time, the test log is stored by day configuration.
Spring Boot custom log4j2 log file