1. Pom.xml configuration
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<! -- Remove springBoot default logging framework -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<! -- log4j2 dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
Copy the code
2. Configure log4j2 files
Create the log4j2-spring. XML file under resource. If you do not use the default name, add the following configuration to application.yml:
logging:
config: ***.xml
Copy the code
The log4j2 configuration file is as follows:
<configuration status="warn">
<! -- Custom variables -->
<properties>
<Property name="APP_NAME">Your project name</Property>
<Property name="LOG_PATH">Your log file path</Property>
<! -- Output format -->
<Property name="LOG_PATTERN">[%d][%t][%p][%c:%L] %m%n</Property>
</properties>
<appenders>
<console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="[%d][%t][%p][%l] %m%n" />
</console>
<! -- Output info and below information -->
<RollingFile name="RollingFileInfo" fileName="${LOG_PATH}/info.log"
filePattern="${LOG_PATH}/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="INFO" />
<ThresholdFilter level="WARN" onMatch="DENY"
onMismatch="NEUTRAL" />
</Filters>
<! -- Output log format -->
<PatternLayout pattern="${LOG_PATTERN}" />
<Policies>
<! -- File daily documents -->
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
<! -- Single file size -->
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
<! Number of files per day -->
<DefaultRolloverStrategy compressionLevel="0" max="10"/>
</RollingFile>
<! -- Output warn and below levels -->
<RollingFile name="RollingFileWarn" fileName="${LOG_PATH}/warn.log"
filePattern="${LOG_PATH}/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log">
<Filters>
<ThresholdFilter level="WARN" />
<ThresholdFilter level="ERROR" onMatch="DENY"
onMismatch="NEUTRAL" />
</Filters>
<PatternLayout pattern="${LOG_PATTERN}" />
<Policies>
<! -- File daily documents -->
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
<! -- Single file size -->
<SizeBasedTriggeringPolicy size="2 MB" />
</Policies>
<! Number of files per day -->
<DefaultRolloverStrategy compressionLevel="0" max="10"/>
</RollingFile>
<! -- Output error and the following levels of information -->
<RollingFile name="RollingFileError" fileName="${LOG_PATH}/error.log"
filePattern="${LOG_PATH}/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log">
<ThresholdFilter level="ERROR" />
<PatternLayout pattern="[${LOG_PATTERN}" />
<Policies>
<! -- File daily documents -->
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
<! -- Single file size -->
<SizeBasedTriggeringPolicy size="2 MB" />
</Policies>
<! Number of files per day -->
<DefaultRolloverStrategy compressionLevel="0" max="10"/>
</RollingFile>
</appenders>
<loggers>
<root level="info">
<appender-ref ref="Console" />
<appender-ref ref="RollingFileInfo" />
<appender-ref ref="RollingFileWarn" />
<appender-ref ref="RollingFileError" />
</root>
</loggers>
</configuration>
Copy the code
3. Simple use
public class Test {
private Logger logger = LoggerFactory.getLogger(Test.class);
public void test(a) {
logger.info("Info level logs");
logger.warn("Warn level logs");
logger.error("Error Level Logs"); }}Copy the code