0, modified on the basis of the previous SpringBoot
1, add log4j2 library dependencies, springboot default is logback logging framework, so it need to exclude logback, otherwise there will be conflicts jar dependent, can not write to the log file appears, it can be printed only in the case of the console.
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.13.0'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.0'
configurations {
providedRuntime
all*.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
2, add resources under log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<properties>
<property name="LOG_HOME">D:/logs</property>
</properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="ErrLog" fileName="${LOG_HOME}/error.log" immediateFlush="true" append="true">
<PatternLayout pattern="%d{yy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<Root level="all">
<AppenderRef ref="Console" level="error"/>
<AppenderRef ref="ErrLog" level="error" />
</Root>
</Loggers>
</Configuration>
3, Controller added Log Print
logger.error(String.format(template, name));