forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YunaiV
committed
Jan 1, 2020
1 parent
b30af8d
commit 0af7453
Showing
8 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?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"> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.2.2.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>lab-37-logging-aop</artifactId> | ||
|
||
<dependencies> | ||
<!-- 实现对 Spring MVC 的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<!-- 实现对 Spring AOP 的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-aop</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
13 changes: 13 additions & 0 deletions
13
...lab-37-logging-aop/src/main/java/cn/iocoder/springboot/lab37/loggingdemo/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cn.iocoder.springboot.lab37.loggingdemo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
...ng-aop/src/main/java/cn/iocoder/springboot/lab37/loggingdemo/aspect/HttpAccessAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package cn.iocoder.springboot.lab37.loggingdemo.aspect; | ||
|
||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Aspect | ||
public class HttpAccessAspect { | ||
|
||
private Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
@Around("@within(org.springframework.stereotype.Controller)" + | ||
"|| @within(org.springframework.web.bind.annotation.RestController)") | ||
public Object around(ProceedingJoinPoint point) throws Throwable { | ||
// 获取类名 | ||
String className = point.getTarget().getClass().getName(); | ||
// 获取方法 | ||
String methodName = point.getSignature().getName(); | ||
// 记录开始时间 | ||
long beginTime = System.currentTimeMillis(); | ||
// 记录返回结果 | ||
Object result = null; | ||
Exception ex = null; | ||
try { | ||
// 执行方法 | ||
result = point.proceed(); | ||
return result; | ||
} catch (Exception e) { | ||
ex = e; | ||
throw e; | ||
} finally { | ||
// 计算消耗时间 | ||
long costTime = System.currentTimeMillis() - beginTime; | ||
// 发生异常,则打印 ERROR 日志 | ||
if (ex != null) { | ||
logger.error("[className: {}][methodName: {}][cost: {} ms][args: {}][发生异常]", | ||
className, methodName, point.getArgs(), ex); | ||
// 正常执行,则打印 INFO 日志 | ||
} else { | ||
logger.info("[className: {}][methodName: {}][cost: {} ms][args: {}][return: {}]", | ||
className, methodName, costTime, point.getArgs(), result); | ||
} | ||
} | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
...-aop/src/main/java/cn/iocoder/springboot/lab37/loggingdemo/controller/DemoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package cn.iocoder.springboot.lab37.loggingdemo.controller; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/demo") | ||
public class DemoController { | ||
|
||
private Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
@GetMapping("/debug") | ||
public void debug() { | ||
logger.debug("debug"); | ||
} | ||
|
||
@GetMapping("/info") | ||
public void info() { | ||
logger.info("info"); | ||
} | ||
|
||
@GetMapping("/error") | ||
public void error() { | ||
logger.error("error"); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
lab-37/lab-37-logging-aop/src/main/resources/application.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
spring: | ||
application: | ||
name: demo-application # 应用名 | ||
|
||
logging: | ||
# 日志文件配置 | ||
file: | ||
# path: /Users/yunai/logs/ # 日志文件路径。 | ||
name: /Users/yunai/logs/${spring.application.name}.log # 日志文件名。 | ||
max-history: 7 # 日志文件要保留的归档的最大天数。默认为 7 天。 | ||
max-size: 10MB # 日志文件的最大大小。默认为 10MB 。 | ||
|
||
# 日志级别 | ||
level: | ||
cn: | ||
iocoder: | ||
springboot: | ||
lab37: | ||
loggingdemo: | ||
controller: DEBUG |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
spring: | ||
application: | ||
name: demo-application # 应用名 | ||
|
||
logging: | ||
# 日志文件配置 | ||
file: | ||
# path: /Users/yunai/logs/ # 日志文件路径。 | ||
name: /Users/yunai/logs/${spring.application.name}.log # 日志文件名。 | ||
max-history: 7 # 日志文件要保留的归档的最大天数。默认为 7 天。 | ||
max-size: 10MB # 日志文件的最大大小。默认为 10MB 。 | ||
|
||
# 日志级别 | ||
level: | ||
cn: | ||
iocoder: | ||
springboot: | ||
lab37: | ||
loggingdemo: | ||
controller: DEBUG |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters