Skip to content

Commit 46a345e

Browse files
committed
add GeneralLog:
define AspectLog for generalize log in whole project
1 parent dbadf70 commit 46a345e

4 files changed

Lines changed: 55 additions & 0 deletions

File tree

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@
7777
<scope>runtime</scope>
7878
</dependency>
7979

80+
<!-- AOP -->
81+
<dependency>
82+
<groupId>org.springframework.boot</groupId>
83+
<artifactId>spring-boot-starter-aop</artifactId>
84+
</dependency>
85+
8086
<!--use cache dependency -->
8187
<dependency>
8288
<groupId>org.springframework.boot</groupId>

src/main/java/ir/bigz/springbootreal/SpringbootRealApplication.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import org.springframework.transaction.annotation.EnableTransactionManagement;
1010

1111
//TODO add pagination to dao
12+
//TODO add validation
13+
//TODO complex model
14+
//TODO global log
1215

1316
@SpringBootApplication(exclude = {
1417
DataSourceAutoConfiguration.class,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package ir.bigz.springbootreal.commons.generallog;
2+
3+
import org.aspectj.lang.JoinPoint;
4+
import org.aspectj.lang.annotation.AfterReturning;
5+
import org.aspectj.lang.annotation.Aspect;
6+
import org.aspectj.lang.annotation.Before;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.stereotype.Component;
11+
12+
import java.util.Arrays;
13+
14+
@Aspect
15+
@Component
16+
public class AppLogAspect {
17+
18+
private final Logger LOG = LoggerFactory.getLogger(AppLogAspect.class);
19+
20+
@Before("ir.bigz.springbootreal.commons.generallog.CommonJoinPoint.ControllerExecution()")
21+
public void before(JoinPoint joinPoint){
22+
String methodName = joinPoint.getSignature().getName();
23+
Object[] args = joinPoint.getArgs();
24+
String reduce = Arrays.stream(args).reduce("", (s, s2) -> s + " " + s2).toString();
25+
LOG.info("before method: {} | argument: {}",
26+
methodName, reduce);
27+
}
28+
29+
@AfterReturning(value = "ir.bigz.springbootreal.commons.generallog.CommonJoinPoint.ControllerExecution()",
30+
returning = "obj")
31+
public void afterReturning(JoinPoint joinPoint, Object obj){
32+
String methodName = joinPoint.getSignature().getName();
33+
Object[] args = joinPoint.getArgs();
34+
String reduce = Arrays.stream(args).reduce("", (s, s2) -> s + " " + s2).toString();
35+
LOG.info("after method: {} | argument: {} | result: {}", methodName, reduce, ((ResponseEntity) obj).getBody());
36+
}
37+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package ir.bigz.springbootreal.commons.generallog;
2+
3+
import org.aspectj.lang.annotation.Pointcut;
4+
5+
public class CommonJoinPoint {
6+
7+
@Pointcut("execution(* ir.bigz.springbootreal.controller.*.*(..))")
8+
public void ControllerExecution(){};
9+
}

0 commit comments

Comments
 (0)