1.pom引入
<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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
2.log4j2.xml配置
log4j2.xml需要放在resources中
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="OFF" monitorInterval="1800">
<!-- 该配置文件只可修改 baseLogPath 和 applicationName 其他值请勿动 -->
<Properties>
<Property name="rootLoggerLevel">INFO</Property>
<Property name="bizLoggerLevel">INFO</Property>
<Property name="warnLoggerLevel">WARN</Property>
<Property name="exceptionLoggerLevel">ERROR</Property>
<!-- 日志根目录 -->
<Property name="baseLogPath">logs/</Property>
<!-- 日志格式 -->
<Property name="logPattern">%d{yyyy-MM-dd HH:mm:ss:SSS} - %p - %msg%ex%n</Property>
<Property name="applicationName">applicationName</Property>
</Properties>
<Appenders>
<!-- 控制台输出,请在调试环境下使用 -->
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${logPattern}"/>
</Console>
<!-- 日志信息输出到文件配置 -->
<RollingFile name="RollingFileInfo" fileName="${baseLogPath}/${applicationName}.log"
filePattern="${baseLogPath}/INFO/app-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout pattern="${logPattern}" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="50 MB"/>
</Policies>
<DefaultRolloverStrategy max="100">
<Delete basePath="${baseLogPath}" maxDepth="2">
<IfFileName glob="*/app-*.log.gz">
<IfLastModified age="7d">
</IfLastModified>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
<RollingFile name="RollingFileError" fileName="${baseLogPath}/error.log"
filePattern="${baseLogPath}/error/error-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout pattern="${logPattern}" />
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
<Policies>
<!--每天分割一次-->
<TimeBasedTriggeringPolicy />
<!--文件每满50MB分割一次-->
<SizeBasedTriggeringPolicy size="50 MB"/>
</Policies>
<DefaultRolloverStrategy max="100">
<Delete basePath="${baseLogPath}" maxDepth="2">
<IfFileName glob="*/error-*.log.gz">
<IfLastModified age="7d">
</IfLastModified>
</IfFileName>
</Delete>
</DefaultRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<logger name="com.mmtrix.dao" level="debug" additivity="false">
<appender-ref ref="Console"></appender-ref>
</logger>
<Root level="${rootLoggerLevel}">
<appender-ref ref="Console"/>
<appender-ref ref="RollingFileInfo"/>
<appender-ref ref="RollingFileError"/>
</Root>
</Loggers>
</Configuration>
3.使用
private static Logger logger = LoggerFactory.getLogger(DemoApplication.class);
public static void main(String[] args) {
logger.info("系统启动中...");
SpringApplication.run(DemoApplication.class, args);
logger.info("系统启动成功");
}