Skip to content

Commit

Permalink
Add: [#17] 로그 추적 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Red-Gunny committed Jun 15, 2024
1 parent 42131e8 commit 8821131
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
53 changes: 53 additions & 0 deletions blog/src/main/java/cloudclub/blog/common/log/BlogLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cloudclub.blog.common.log;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.lang.reflect.Method;

@Aspect
@Slf4j
public class BlogLogger {

@Pointcut("execution(* cloudclub.blog.*.*(..))")
private void publicTarget(){
}


@Before("publicTarget()")
public void beforeController(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
String requestId = request.getRequestId();
String className = joinPoint.getTarget().getClass().getSimpleName();
log.info("[{}] #BEFORE >> {}.{}()", requestId, className, method.getName());
Object[] args = joinPoint.getArgs();
for(Object arg : args) {
if(arg != null) {
log.info(" type = {}", arg.getClass().getSimpleName());
log.info(" value = {}", arg);
}
}
}

@After("publicTarget()")
public void afterController(JoinPoint joinPoint) {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = methodSignature.getMethod();

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
String requestId = request.getRequestId();
String className = joinPoint.getTarget().getClass().getSimpleName();
log.info("[{}] #After >> {}.{}()", requestId, className, method.getName());
}

}
16 changes: 16 additions & 0 deletions blog/src/main/java/cloudclub/blog/common/log/LoggerConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cloudclub.blog.common.log;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@EnableAspectJAutoProxy
public class LoggerConfig {

@Bean
public BlogLogger blogLogger() {
return new BlogLogger();
}

}

0 comments on commit 8821131

Please sign in to comment.