From 088f39bcf84c19cc5dde852a3c6d831d47ed1bd8 Mon Sep 17 00:00:00 2001
From: "Yangkai.Shen" <237497819@qq.com>
Date: Mon, 1 Oct 2018 22:37:37 +0800
Subject: [PATCH] =?UTF-8?q?:sparkles:=20spring-boot-demo-log-aop=20?=
=?UTF-8?q?=E5=AE=8C=E6=88=90?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
spring-boot-demo-log-aop/.gitignore | 25 +++
spring-boot-demo-log-aop/README.md | 201 ++++++++++++++++++
spring-boot-demo-log-aop/pom.xml | 75 +++++++
.../aop/SpringBootDemoLogAopApplication.java | 25 +++
.../com/xkcoding/log/aop/aspectj/AopLog.java | 95 +++++++++
.../log/aop/controller/TestController.java | 35 +++
.../src/main/resources/application.yml | 4 +
.../src/main/resources/logback-spring.xml | 77 +++++++
.../SpringBootDemoLogAopApplicationTests.java | 16 ++
9 files changed, 553 insertions(+)
create mode 100644 spring-boot-demo-log-aop/.gitignore
create mode 100644 spring-boot-demo-log-aop/README.md
create mode 100644 spring-boot-demo-log-aop/pom.xml
create mode 100644 spring-boot-demo-log-aop/src/main/java/com/xkcoding/log/aop/SpringBootDemoLogAopApplication.java
create mode 100644 spring-boot-demo-log-aop/src/main/java/com/xkcoding/log/aop/aspectj/AopLog.java
create mode 100644 spring-boot-demo-log-aop/src/main/java/com/xkcoding/log/aop/controller/TestController.java
create mode 100644 spring-boot-demo-log-aop/src/main/resources/application.yml
create mode 100644 spring-boot-demo-log-aop/src/main/resources/logback-spring.xml
create mode 100644 spring-boot-demo-log-aop/src/test/java/com/xkcoding/log/aop/SpringBootDemoLogAopApplicationTests.java
diff --git a/spring-boot-demo-log-aop/.gitignore b/spring-boot-demo-log-aop/.gitignore
new file mode 100644
index 0000000..82eca33
--- /dev/null
+++ b/spring-boot-demo-log-aop/.gitignore
@@ -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/
\ No newline at end of file
diff --git a/spring-boot-demo-log-aop/README.md b/spring-boot-demo-log-aop/README.md
new file mode 100644
index 0000000..8908633
--- /dev/null
+++ b/spring-boot-demo-log-aop/README.md
@@ -0,0 +1,201 @@
+# spring-boot-demo-log-aop
+
+> 此 demo 主要是演示如何使用 aop 切面对请求进行日志记录,并且记录 UserAgent 信息。
+
+## pom.xml
+
+```xml
+
+
+ * 使用 aop 切面记录请求日志信息 + *
+ * + * @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+ * 测试 Controller + *
+ * + * @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); + } + +} +``` + diff --git a/spring-boot-demo-log-aop/pom.xml b/spring-boot-demo-log-aop/pom.xml new file mode 100644 index 0000000..d2b07a3 --- /dev/null +++ b/spring-boot-demo-log-aop/pom.xml @@ -0,0 +1,75 @@ + ++ * 启动类 + *
+ * + * @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); + } +} diff --git a/spring-boot-demo-log-aop/src/main/java/com/xkcoding/log/aop/aspectj/AopLog.java b/spring-boot-demo-log-aop/src/main/java/com/xkcoding/log/aop/aspectj/AopLog.java new file mode 100644 index 0000000..9caa858 --- /dev/null +++ b/spring-boot-demo-log-aop/src/main/java/com/xkcoding/log/aop/aspectj/AopLog.java @@ -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; + +/** + *+ * 使用 aop 切面记录请求日志信息 + *
+ * + * @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+ * 测试 Controller + *
+ * + * @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); + } + +} diff --git a/spring-boot-demo-log-aop/src/main/resources/application.yml b/spring-boot-demo-log-aop/src/main/resources/application.yml new file mode 100644 index 0000000..af5002c --- /dev/null +++ b/spring-boot-demo-log-aop/src/main/resources/application.yml @@ -0,0 +1,4 @@ +server: + port: 8080 + servlet: + context-path: /demo diff --git a/spring-boot-demo-log-aop/src/main/resources/logback-spring.xml b/spring-boot-demo-log-aop/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..284bb16 --- /dev/null +++ b/spring-boot-demo-log-aop/src/main/resources/logback-spring.xml @@ -0,0 +1,77 @@ + +