-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from SWM-Flash/feature/FLASH-246-API-logging
Spring AOP를 활용한 logging
- Loading branch information
Showing
18 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
src/main/java/com/first/flash/account/member/domain/Gender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/main/java/com/first/flash/account/member/domain/Role.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/main/java/com/first/flash/global/aspect/LoggingAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
src/main/java/com/first/flash/global/aspect/PointCuts.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
|
||
} | ||
} |