Skip to content

Commit

Permalink
Merge pull request #52 from SWM-Flash/feature/FLASH-246-API-logging
Browse files Browse the repository at this point in the history
Spring AOP를 활용한 logging
  • Loading branch information
ChoiWonYu authored Sep 4, 2024
2 parents f1456da + 1eaa600 commit 997bdc6
Show file tree
Hide file tree
Showing 18 changed files with 160 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.first.flash.account.member.domain;

import lombok.ToString;

@ToString
public enum Gender {

MALE, FEMALE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@NoArgsConstructor(access = lombok.AccessLevel.PROTECTED)
@AllArgsConstructor
@Builder
@Getter
@ToString
public class Member {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import jakarta.persistence.ManyToOne;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@Getter
@NoArgsConstructor
@ToString
public class MemberBlock extends BaseEntity {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import jakarta.persistence.ManyToOne;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@Getter
@NoArgsConstructor
@ToString
public class MemberReport extends BaseEntity {

@Id
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/first/flash/account/member/domain/Role.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.first.flash.account.member.domain;

import lombok.AllArgsConstructor;
import lombok.ToString;

@AllArgsConstructor
@ToString
public enum Role {

ROLE_ADMIN("ROLE_ADMIN"), ROLE_USER("ROLE_USER");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@NoArgsConstructor
@AllArgsConstructor
@Getter
@ToString
public class ClimbingGym {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Embeddable
@NoArgsConstructor
@EqualsAndHashCode
@Getter
@ToString
public class Difficulty {

@NotEmpty(message = "난이도 이름은 필수입니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@Getter
@ToString
public class Problem {

private static final Long DEFAULT_OPTIONAL_WEIGHT = 0L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@NoArgsConstructor
Expand All @@ -27,6 +28,7 @@
columnList = "isExpired, recommendationValue, id")
}
)
@ToString
public class QueryProblem {

private static final Boolean DEFAULT_HAS_SOLUTION = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Embeddable
@Getter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class DifficultyInfo {

private String difficultyName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@NoArgsConstructor
@AllArgsConstructor
@Getter
@ToString
public class Sector {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Embeddable
@NoArgsConstructor
@EqualsAndHashCode
@Getter
@ToString
public class RemovalInfo {

private static final int FAKE_SECTOR_LIFETIME_DAYS = 30;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Embeddable
@NoArgsConstructor
@EqualsAndHashCode
@Getter
@ToString
public class SectorName {
private String name;
private String adminName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@NoArgsConstructor
@AllArgsConstructor
@Getter
@ToString
public class Solution extends BaseEntity {

private static final Long DEFAULT_OPTIONAL_WEIGHT = 0L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Embeddable
@NoArgsConstructor
@EqualsAndHashCode
@Getter
@ToString
public class SolutionDetail {

private String review;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Embeddable
@NoArgsConstructor
@EqualsAndHashCode
@Getter
@ToString
public class UploaderDetail {

@Column(columnDefinition = "BINARY(16)")
Expand Down
104 changes: 104 additions & 0 deletions src/main/java/com/first/flash/global/aspect/LoggingAspect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.first.flash.global.aspect;

import com.first.flash.global.util.AuthUtil;
import jakarta.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Aspect
@Slf4j
@Component
@RequiredArgsConstructor
public class LoggingAspect {

@Before("com.first.flash.global.aspect.PointCuts.allController()")
public void doBeforeController() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
.getRequest();

log.info("======= Controller Start =======");
log.info("Request URI: {}", request.getRequestURI());
log.info("HTTP Method: {}", request.getMethod());

Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
log.info("Header: {} = {}", headerName, request.getHeader(headerName));
}

Enumeration<String> parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()) {
String parameterName = parameterNames.nextElement();
log.info("Parameter: {} = {}", parameterName, request.getParameter(parameterName));
}
}

@Before("com.first.flash.global.aspect.PointCuts.verifiedControllers()")
public void doBeforeVerifiedController() {
log.info("user id: {}", AuthUtil.getId());
}

@AfterReturning(value = "com.first.flash.global.aspect.PointCuts.allController()", returning = "result")
public void afterReturnController(final Object result) {
if (result instanceof ResponseEntity<?> responseEntity) {
int statusCode = responseEntity.getStatusCode().value();
Object responseBody = responseEntity.getBody();

log.info("Response Status Code: {}", statusCode);
if (!Objects.isNull(responseBody)) {
log.info("Response Body: {}", responseBody);
} else {
log.info("no Response Body");
}
} else {
if (!Objects.isNull(result)) {
log.info("Response: {}", result);
} else {
log.info("no Response");
}
}
log.info("======= Controller End =======");
}

@Before("com.first.flash.global.aspect.PointCuts.allService() || com.first.flash.global.aspect.PointCuts.allRepository()")
public void doBefore(final JoinPoint joinPoint) {
Method method = getMethod(joinPoint);
String objectName = joinPoint.getTarget()
.getClass()
.getSimpleName();
log.info("======= {} Start =======", objectName);
log.info("method name = {}", method.getName());
Object[] args = joinPoint.getArgs();
for (Object arg : args) {
log.info("parameter type = {}", arg.getClass().getSimpleName());
log.info("parameter value = {}", arg);
}
}

@AfterReturning(value = "com.first.flash.global.aspect.PointCuts.allService() || com.first.flash.global.aspect.PointCuts.allRepository()", returning = "result")
public void afterReturnLog(final JoinPoint joinPoint, final Object result) {
log.info("return type = {}", result.getClass().getSimpleName());
log.info("return value = {}", result);
String objectName = joinPoint.getTarget()
.getClass()
.getSimpleName();
log.info("======= {} End =======", objectName);
}

private Method getMethod(final JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
return signature.getMethod();
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/first/flash/global/aspect/PointCuts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.first.flash.global.aspect;

import org.aspectj.lang.annotation.Pointcut;

public class PointCuts {

@Pointcut("execution(* com.first.flash..*Controller.*(..))")
public void allController() {
}

@Pointcut("execution(* com.first.flash..*Service*.*(..))")
public void allService() {
}

@Pointcut("execution(* com.first.flash..*Repository*.*(..))")
public void allRepository() {
}

@Pointcut("execution(* com.first.flash.climbing..*Controller.*(..))")
public void verifiedControllers() {

}
}

0 comments on commit 997bdc6

Please sign in to comment.