版本
spring boot:2.0.3.RELEASE
1、引入maven依賴
<!--log4j2 start 不使用spring boot自帶的logback-->
<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</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--log4j2 end-->
2、在src\main\resources\下新建log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--如果程序運行過程中,配置信息發生變化,每30秒動態加載一次-->
<Configuration status="warn" monitorInterval="30">
<!--自定義變量,後面可以使用${}引入變量-->
<properties>
<property name="path">./logs</property>
<property name="charset">UTF-8</property>
<!--%t 線程name-->
<!--%F 生成日誌所在的類名-->
<!--%L 生成日誌所在的行號-->
<!--%m 具體日誌信息-->
<!--%n 換行符-->
<property name="pattern">%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n</property>
</properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout charset="${charset}" pattern="${pattern}" />
</Console>
<RollingFile name="InfoRollingFile" fileName="${path}/message.log" filePattern="${path}/$${date:yyyy-MM-dd}/message-%d{yyyy-MM-dd}-%i.log">
<!-- 記錄INFO以及更高級別的日誌(WARN/ERROR)-->
<!-- 日誌級別從低到高:-->
<!-- debug < info < warn < error < fatal -->
<ThresholdFilter level="info"/>
<PatternLayout charset="${charset}" pattern="${pattern}" />
<Policies>
<!--每次服務啓動或重啓時,生成一個新日誌文件-->
<OnStartupTriggeringPolicy />
<!--第二天時,生成一個新的日誌文件-->
<TimeBasedTriggeringPolicy />
<!--日誌達到16M時,生成一個新日誌文件-->
<SizeBasedTriggeringPolicy size="16 MB" />
</Policies>
</RollingFile>
<RollingFile name="ErrorRollingFile" fileName="${path}/error.log" filePattern="${path}/$${date:yyyy-MM-dd}/error-%d{yyyy-MM-dd}-%i.log">
<!-- 記錄ERROR日誌-->
<ThresholdFilter level="error"/>
<PatternLayout charset="${charset}" pattern="${pattern}" />
<Policies>
<OnStartupTriggeringPolicy />
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="16 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="InfoRollingFile"/>
<AppenderRef ref="ErrorRollingFile"/>
</Root>
<logger name="org.springframework" level="info"></logger>
<logger name="org.apache" level="info"></logger>
</Loggers>
</Configuration>
3、寫一個controller測試一下
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
private static Logger logger = LogManager.getLogger(TestController.class);
@RequestMapping(value = "/logs", method = RequestMethod.GET)
public void fun(String name) {
logger.debug("fun()方法入參:{}", name);
logger.info("fun()方法入參:{}", name);
logger.warn("fun()方法入參:{}", name);
logger.error("fun()方法入參:{}", name);
}
}
啓動,地址欄輸入http://localhost:8080/logs?name=123