Skip to content

Commit

Permalink
增加 log 相关的示例
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Jan 1, 2020
1 parent b30af8d commit 0af7453
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lab-37/lab-37-logging-aop/pom.xml
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>
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);
}

}
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);
}
}
}

}
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 lab-37/lab-37-logging-aop/src/main/resources/application.yaml
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
20 changes: 20 additions & 0 deletions lab-37/lab-37-logging-aop/target/classes/application.yaml
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
1 change: 1 addition & 0 deletions lab-37/lab-37-logging-log4j2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
</exclusions>
</dependency>

<!-- 实现对 SLF4J + Log4j2 的依赖的引入 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
Expand Down
1 change: 1 addition & 0 deletions lab-37/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<module>lab-37-logging-debug</module>
<module>lab-37-logging-multi-env</module>
<module>lab-37-logging-log4j2</module>
<module>lab-37-logging-aop</module>
</modules>


Expand Down

0 comments on commit 0af7453

Please sign in to comment.