@@ -0,0 +1,25 @@ | |||||
/target/ | |||||
!.mvn/wrapper/maven-wrapper.jar | |||||
### STS ### | |||||
.apt_generated | |||||
.classpath | |||||
.factorypath | |||||
.project | |||||
.settings | |||||
.springBeans | |||||
.sts4-cache | |||||
### IntelliJ IDEA ### | |||||
.idea | |||||
*.iws | |||||
*.iml | |||||
*.ipr | |||||
### NetBeans ### | |||||
/nbproject/private/ | |||||
/build/ | |||||
/nbbuild/ | |||||
/dist/ | |||||
/nbdist/ | |||||
/.nb-gradle/ |
@@ -0,0 +1,201 @@ | |||||
# spring-boot-demo-log-aop | |||||
> 此 demo 主要是演示如何使用 aop 切面对请求进行日志记录,并且记录 UserAgent 信息。 | |||||
## pom.xml | |||||
```xml | |||||
<?xml version="1.0" encoding="UTF-8"?> | |||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |||||
<modelVersion>4.0.0</modelVersion> | |||||
<groupId>com.xkcoding</groupId> | |||||
<artifactId>spring-boot-demo-log-aop</artifactId> | |||||
<version>0.0.1-SNAPSHOT</version> | |||||
<packaging>jar</packaging> | |||||
<name>spring-boot-demo-log-aop</name> | |||||
<description>Demo project for Spring Boot</description> | |||||
<parent> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-parent</artifactId> | |||||
<version>2.0.5.RELEASE</version> | |||||
<relativePath/> <!-- lookup parent from repository --> | |||||
</parent> | |||||
<properties> | |||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |||||
<java.version>1.8</java.version> | |||||
<hutool.version>4.1.14</hutool.version> | |||||
<user.agent.version>1.20</user.agent.version> | |||||
</properties> | |||||
<dependencies> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-web</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-aop</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-test</artifactId> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.projectlombok</groupId> | |||||
<artifactId>lombok</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>cn.hutool</groupId> | |||||
<artifactId>hutool-all</artifactId> | |||||
<version>${hutool.version}</version> | |||||
</dependency> | |||||
<!-- 解析 UserAgent 信息 --> | |||||
<dependency> | |||||
<groupId>eu.bitwalker</groupId> | |||||
<artifactId>UserAgentUtils</artifactId> | |||||
<version>${user.agent.version}</version> | |||||
</dependency> | |||||
</dependencies> | |||||
<build> | |||||
<finalName>spring-boot-demo-log-aop</finalName> | |||||
<plugins> | |||||
<plugin> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-maven-plugin</artifactId> | |||||
</plugin> | |||||
</plugins> | |||||
</build> | |||||
</project> | |||||
``` | |||||
## AopLog.java | |||||
```java | |||||
/** | |||||
* <p> | |||||
* 使用 aop 切面记录请求日志信息 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.log.aop.aspectj | |||||
* @description: 使用 aop 切面记录请求日志信息 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/10/1 10:05 PM | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Aspect | |||||
@Component | |||||
@Slf4j | |||||
public class AopLog { | |||||
private static final String START_TIME = "request-start"; | |||||
/** | |||||
* 切入点 | |||||
*/ | |||||
@Pointcut("execution(public * com.xkcoding.log.aop.controller.*Controller.*(..))") | |||||
public void log() { | |||||
} | |||||
/** | |||||
* 前置操作 | |||||
* | |||||
* @param point 切入点 | |||||
*/ | |||||
@Before("log()") | |||||
public void beforeLog(JoinPoint point) { | |||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); | |||||
HttpServletRequest request = Objects.requireNonNull(attributes).getRequest(); | |||||
log.info("【请求 URL】:{}", request.getRequestURL()); | |||||
log.info("【请求 IP】:{}", request.getRemoteAddr()); | |||||
log.info("【请求类名】:{},【请求方法名】:{}", point.getSignature().getDeclaringTypeName(), point.getSignature().getName()); | |||||
Map<String, String[]> parameterMap = request.getParameterMap(); | |||||
log.info("【请求参数】:{},", JSONUtil.toJsonStr(parameterMap)); | |||||
Long start = System.currentTimeMillis(); | |||||
request.setAttribute(START_TIME, start); | |||||
} | |||||
/** | |||||
* 环绕操作 | |||||
* | |||||
* @param point 切入点 | |||||
* @return 原方法返回值 | |||||
* @throws Throwable 异常信息 | |||||
*/ | |||||
@Around("log()") | |||||
public Object aroundLog(ProceedingJoinPoint point) throws Throwable { | |||||
Object result = point.proceed(); | |||||
log.info("【返回值】:{}", JSONUtil.toJsonStr(result)); | |||||
return result; | |||||
} | |||||
/** | |||||
* 后置操作 | |||||
*/ | |||||
@AfterReturning("log()") | |||||
public void afterReturning() { | |||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); | |||||
HttpServletRequest request = Objects.requireNonNull(attributes).getRequest(); | |||||
Long start = (Long) request.getAttribute(START_TIME); | |||||
Long end = System.currentTimeMillis(); | |||||
log.info("【请求耗时】:{}毫秒", end - start); | |||||
String header = request.getHeader("User-Agent"); | |||||
UserAgent userAgent = UserAgent.parseUserAgentString(header); | |||||
log.info("【浏览器类型】:{},【操作系统】:{},【原始User-Agent】:{}", userAgent.getBrowser().toString(), userAgent.getOperatingSystem().toString(), header); | |||||
} | |||||
} | |||||
``` | |||||
## TestController.java | |||||
```java | |||||
/** | |||||
* <p> | |||||
* 测试 Controller | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.log.aop.controller | |||||
* @description: 测试 Controller | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/10/1 10:10 PM | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@RestController | |||||
public class TestController { | |||||
/** | |||||
* 测试方法 | |||||
* | |||||
* @param who 测试参数 | |||||
* @return {@link Dict} | |||||
*/ | |||||
@GetMapping("/test") | |||||
public Dict test(String who) { | |||||
return Dict.create().set("who", StrUtil.isBlank(who) ? "me" : who); | |||||
} | |||||
} | |||||
``` | |||||
@@ -0,0 +1,75 @@ | |||||
<?xml version="1.0" encoding="UTF-8"?> | |||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |||||
<modelVersion>4.0.0</modelVersion> | |||||
<groupId>com.xkcoding</groupId> | |||||
<artifactId>spring-boot-demo-log-aop</artifactId> | |||||
<version>0.0.1-SNAPSHOT</version> | |||||
<packaging>jar</packaging> | |||||
<name>spring-boot-demo-log-aop</name> | |||||
<description>Demo project for Spring Boot</description> | |||||
<parent> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-parent</artifactId> | |||||
<version>2.0.5.RELEASE</version> | |||||
<relativePath/> <!-- lookup parent from repository --> | |||||
</parent> | |||||
<properties> | |||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |||||
<java.version>1.8</java.version> | |||||
<hutool.version>4.1.14</hutool.version> | |||||
<user.agent.version>1.20</user.agent.version> | |||||
</properties> | |||||
<dependencies> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-web</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-aop</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-test</artifactId> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.projectlombok</groupId> | |||||
<artifactId>lombok</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>cn.hutool</groupId> | |||||
<artifactId>hutool-all</artifactId> | |||||
<version>${hutool.version}</version> | |||||
</dependency> | |||||
<!-- 解析 UserAgent 信息 --> | |||||
<dependency> | |||||
<groupId>eu.bitwalker</groupId> | |||||
<artifactId>UserAgentUtils</artifactId> | |||||
<version>${user.agent.version}</version> | |||||
</dependency> | |||||
</dependencies> | |||||
<build> | |||||
<finalName>spring-boot-demo-log-aop</finalName> | |||||
<plugins> | |||||
<plugin> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-maven-plugin</artifactId> | |||||
</plugin> | |||||
</plugins> | |||||
</build> | |||||
</project> |
@@ -0,0 +1,25 @@ | |||||
package com.xkcoding.log.aop; | |||||
import org.springframework.boot.SpringApplication; | |||||
import org.springframework.boot.autoconfigure.SpringBootApplication; | |||||
/** | |||||
* <p> | |||||
* 启动类 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.log.aop | |||||
* @description: 启动类 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/10/1 10:05 PM | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@SpringBootApplication | |||||
public class SpringBootDemoLogAopApplication { | |||||
public static void main(String[] args) { | |||||
SpringApplication.run(SpringBootDemoLogAopApplication.class, args); | |||||
} | |||||
} |
@@ -0,0 +1,95 @@ | |||||
package com.xkcoding.log.aop.aspectj; | |||||
import cn.hutool.json.JSONUtil; | |||||
import eu.bitwalker.useragentutils.UserAgent; | |||||
import lombok.extern.slf4j.Slf4j; | |||||
import org.aspectj.lang.JoinPoint; | |||||
import org.aspectj.lang.ProceedingJoinPoint; | |||||
import org.aspectj.lang.annotation.*; | |||||
import org.springframework.stereotype.Component; | |||||
import org.springframework.web.context.request.RequestContextHolder; | |||||
import org.springframework.web.context.request.ServletRequestAttributes; | |||||
import javax.servlet.http.HttpServletRequest; | |||||
import java.util.Map; | |||||
import java.util.Objects; | |||||
/** | |||||
* <p> | |||||
* 使用 aop 切面记录请求日志信息 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.log.aop.aspectj | |||||
* @description: 使用 aop 切面记录请求日志信息 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/10/1 10:05 PM | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Aspect | |||||
@Component | |||||
@Slf4j | |||||
public class AopLog { | |||||
private static final String START_TIME = "request-start"; | |||||
/** | |||||
* 切入点 | |||||
*/ | |||||
@Pointcut("execution(public * com.xkcoding.log.aop.controller.*Controller.*(..))") | |||||
public void log() { | |||||
} | |||||
/** | |||||
* 前置操作 | |||||
* | |||||
* @param point 切入点 | |||||
*/ | |||||
@Before("log()") | |||||
public void beforeLog(JoinPoint point) { | |||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); | |||||
HttpServletRequest request = Objects.requireNonNull(attributes).getRequest(); | |||||
log.info("【请求 URL】:{}", request.getRequestURL()); | |||||
log.info("【请求 IP】:{}", request.getRemoteAddr()); | |||||
log.info("【请求类名】:{},【请求方法名】:{}", point.getSignature().getDeclaringTypeName(), point.getSignature().getName()); | |||||
Map<String, String[]> parameterMap = request.getParameterMap(); | |||||
log.info("【请求参数】:{},", JSONUtil.toJsonStr(parameterMap)); | |||||
Long start = System.currentTimeMillis(); | |||||
request.setAttribute(START_TIME, start); | |||||
} | |||||
/** | |||||
* 环绕操作 | |||||
* | |||||
* @param point 切入点 | |||||
* @return 原方法返回值 | |||||
* @throws Throwable 异常信息 | |||||
*/ | |||||
@Around("log()") | |||||
public Object aroundLog(ProceedingJoinPoint point) throws Throwable { | |||||
Object result = point.proceed(); | |||||
log.info("【返回值】:{}", JSONUtil.toJsonStr(result)); | |||||
return result; | |||||
} | |||||
/** | |||||
* 后置操作 | |||||
*/ | |||||
@AfterReturning("log()") | |||||
public void afterReturning() { | |||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); | |||||
HttpServletRequest request = Objects.requireNonNull(attributes).getRequest(); | |||||
Long start = (Long) request.getAttribute(START_TIME); | |||||
Long end = System.currentTimeMillis(); | |||||
log.info("【请求耗时】:{}毫秒", end - start); | |||||
String header = request.getHeader("User-Agent"); | |||||
UserAgent userAgent = UserAgent.parseUserAgentString(header); | |||||
log.info("【浏览器类型】:{},【操作系统】:{},【原始User-Agent】:{}", userAgent.getBrowser().toString(), userAgent.getOperatingSystem().toString(), header); | |||||
} | |||||
} |
@@ -0,0 +1,35 @@ | |||||
package com.xkcoding.log.aop.controller; | |||||
import cn.hutool.core.lang.Dict; | |||||
import cn.hutool.core.util.StrUtil; | |||||
import org.springframework.web.bind.annotation.GetMapping; | |||||
import org.springframework.web.bind.annotation.RestController; | |||||
/** | |||||
* <p> | |||||
* 测试 Controller | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.log.aop.controller | |||||
* @description: 测试 Controller | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/10/1 10:10 PM | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@RestController | |||||
public class TestController { | |||||
/** | |||||
* 测试方法 | |||||
* | |||||
* @param who 测试参数 | |||||
* @return {@link Dict} | |||||
*/ | |||||
@GetMapping("/test") | |||||
public Dict test(String who) { | |||||
return Dict.create().set("who", StrUtil.isBlank(who) ? "me" : who); | |||||
} | |||||
} |
@@ -0,0 +1,4 @@ | |||||
server: | |||||
port: 8080 | |||||
servlet: | |||||
context-path: /demo |
@@ -0,0 +1,77 @@ | |||||
<?xml version="1.0" encoding="UTF-8"?> | |||||
<configuration> | |||||
<include resource="org/springframework/boot/logging/logback/defaults.xml"/> | |||||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> | |||||
<filter class="ch.qos.logback.classic.filter.LevelFilter"> | |||||
<level>INFO</level> | |||||
</filter> | |||||
<encoder> | |||||
<pattern>%date [%thread] %-5level [%logger{50}] %file:%line - %msg%n</pattern> | |||||
<charset>UTF-8</charset> | |||||
</encoder> | |||||
</appender> | |||||
<appender name="FILE_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender"> | |||||
<!--如果只是想要 Info 级别的日志,只是过滤 info 还是会输出 Error 日志,因为 Error 的级别高, 所以我们使用下面的策略,可以避免输出 Error 的日志--> | |||||
<filter class="ch.qos.logback.classic.filter.LevelFilter"> | |||||
<!--过滤 Error--> | |||||
<level>ERROR</level> | |||||
<!--匹配到就禁止--> | |||||
<onMatch>DENY</onMatch> | |||||
<!--没有匹配到就允许--> | |||||
<onMismatch>ACCEPT</onMismatch> | |||||
</filter> | |||||
<!--日志名称,如果没有File 属性,那么只会使用FileNamePattern的文件路径规则如果同时有<File>和<FileNamePattern>,那么当天日志是<File>,明天会自动把今天的日志改名为今天的日期。即,<File> 的日志都是当天的。--> | |||||
<!--<File>logs/info.spring-boot-demo-log-aop.log</File>--> | |||||
<!--滚动策略,按照时间滚动 TimeBasedRollingPolicy--> | |||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> | |||||
<!--文件路径,定义了日志的切分方式——把每一天的日志归档到一个文件中,以防止日志填满整个磁盘空间--> | |||||
<FileNamePattern>logs/spring-boot-demo-log-aop/info.created_on_%d{yyyy-MM-dd}.part_%i.log</FileNamePattern> | |||||
<!--只保留最近90天的日志--> | |||||
<maxHistory>90</maxHistory> | |||||
<!--用来指定日志文件的上限大小,那么到了这个值,就会删除旧的日志--> | |||||
<!--<totalSizeCap>1GB</totalSizeCap>--> | |||||
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> | |||||
<!-- maxFileSize:这是活动文件的大小,默认值是10MB,本篇设置为1KB,只是为了演示 --> | |||||
<maxFileSize>2MB</maxFileSize> | |||||
</timeBasedFileNamingAndTriggeringPolicy> | |||||
</rollingPolicy> | |||||
<!--<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">--> | |||||
<!--<maxFileSize>1KB</maxFileSize>--> | |||||
<!--</triggeringPolicy>--> | |||||
<encoder> | |||||
<pattern>%date [%thread] %-5level [%logger{50}] %file:%line - %msg%n</pattern> | |||||
<charset>UTF-8</charset> <!-- 此处设置字符集 --> | |||||
</encoder> | |||||
</appender> | |||||
<appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender"> | |||||
<!--如果只是想要 Error 级别的日志,那么需要过滤一下,默认是 info 级别的,ThresholdFilter--> | |||||
<filter class="ch.qos.logback.classic.filter.ThresholdFilter"> | |||||
<level>Error</level> | |||||
</filter> | |||||
<!--日志名称,如果没有File 属性,那么只会使用FileNamePattern的文件路径规则如果同时有<File>和<FileNamePattern>,那么当天日志是<File>,明天会自动把今天的日志改名为今天的日期。即,<File> 的日志都是当天的。--> | |||||
<!--<File>logs/error.spring-boot-demo-log-aop.log</File>--> | |||||
<!--滚动策略,按照时间滚动 TimeBasedRollingPolicy--> | |||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> | |||||
<!--文件路径,定义了日志的切分方式——把每一天的日志归档到一个文件中,以防止日志填满整个磁盘空间--> | |||||
<FileNamePattern>logs/spring-boot-demo-log-aop/error.created_on_%d{yyyy-MM-dd}.part_%i.log</FileNamePattern> | |||||
<!--只保留最近90天的日志--> | |||||
<maxHistory>90</maxHistory> | |||||
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> | |||||
<!-- maxFileSize:这是活动文件的大小,默认值是10MB,本篇设置为1KB,只是为了演示 --> | |||||
<maxFileSize>2MB</maxFileSize> | |||||
</timeBasedFileNamingAndTriggeringPolicy> | |||||
</rollingPolicy> | |||||
<encoder> | |||||
<pattern>%date [%thread] %-5level [%logger{50}] %file:%line - %msg%n</pattern> | |||||
<charset>UTF-8</charset> <!-- 此处设置字符集 --> | |||||
</encoder> | |||||
</appender> | |||||
<root level="info"> | |||||
<appender-ref ref="CONSOLE"/> | |||||
<appender-ref ref="FILE_INFO"/> | |||||
<appender-ref ref="FILE_ERROR"/> | |||||
</root> | |||||
</configuration> |
@@ -0,0 +1,16 @@ | |||||
package com.xkcoding.log.aop; | |||||
import org.junit.Test; | |||||
import org.junit.runner.RunWith; | |||||
import org.springframework.boot.test.context.SpringBootTest; | |||||
import org.springframework.test.context.junit4.SpringRunner; | |||||
@RunWith(SpringRunner.class) | |||||
@SpringBootTest | |||||
public class SpringBootDemoLogAopApplicationTests { | |||||
@Test | |||||
public void contextLoads() { | |||||
} | |||||
} |