Skip to content

Commit

Permalink
Merge pull request #179 from team-offonoff/log
Browse files Browse the repository at this point in the history
feat: 컨트롤러단 메서드 실행 전, 실행 후 로그 남긴다
  • Loading branch information
melonturtle authored Feb 26, 2024
2 parents b5f99d7 + f400de7 commit e8d7dca
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/main/java/life/offonoff/ab/config/AopConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package life.offonoff.ab.config;

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

@Configuration
@EnableAspectJAutoProxy
public class AopConfig {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package life.offonoff.ab.web.common.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Optional;

@Component
@Aspect
public class LoggingAspectHandler {
private final Logger log = LoggerFactory.getLogger(this.getClass().getSimpleName());
@Pointcut("execution(* life.offonoff.ab.web.*.*(..))")
private void allControllers() {
}

@Around("allControllers()")
public Object doReturnLogging(ProceedingJoinPoint joinPoint) throws Throwable {
String methodName = joinPoint.getSignature().toShortString();
Object[] arguments = joinPoint.getArgs();

log.info("[BEFORE] METHOD={} PARAMETER={} WILL EXECUTE", methodName, arguments);

Object returnedByMethod = joinPoint.proceed(arguments);

log.info("[RETURNED] METHOD={} PARAMETER={} RETURNED={}", joinPoint.getSignature().toShortString(), joinPoint.getArgs(), returnedByMethod);

return returnedByMethod;
}

@AfterThrowing(value = "allControllers()", throwing = "exception")
public void doExceptionLogging(JoinPoint joinPoint, Exception exception) {
String ip = Optional.ofNullable((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.map(attributes -> attributes.getRequest().getHeader("X-Real-IP"))
.orElse("");
log.error("[EXCEPTION THROWN] METHOD={} PARAMETER={} THREW EXCEPTION={} | ip={}",
joinPoint.getSignature().toShortString(), joinPoint.getArgs(), exception, ip);
}
}

0 comments on commit e8d7dca

Please sign in to comment.