Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ckend into feat/#145
  • Loading branch information
Ji hwan Shin authored and Ji hwan Shin committed Apr 21, 2024
2 parents e5403dd + 6fc114c commit 41f3ea9
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 2 deletions.
2 changes: 1 addition & 1 deletion main/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM openjdk:17-jdk-slim AS build
WORKDIR /app
COPY . /app
RUN ./gradlew build
RUN ./gradlew build -x test

FROM openjdk:17-jdk-slim
WORKDIR /app
Expand Down
3 changes: 2 additions & 1 deletion main/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ dependencies {
testImplementation 'org.testcontainers:jdbc' // DB와의 JDBC connection
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.14.0'


// aop
implementation 'org.springframework.boot:spring-boot-starter-aop'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@SpringBootApplication
@EnableAspectJAutoProxy
public class MainApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.sopt.makers.crew.main.common.aop;

import jakarta.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.Objects;
import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.sopt.makers.crew.main.common.util.UserUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Aspect
@Component
@Log4j2
public class ExecutionLoggingAop {

// 모든 패키지 내의 controller package에 존재하는 클래스
@Around("execution(* org.sopt.makers.crew..*Controller.*(..))")
public Object logExecutionTrace(ProceedingJoinPoint pjp) throws Throwable {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
RequestMethod httpMethod = RequestMethod.valueOf(request.getMethod());

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Integer userId = (Integer) authentication.getPrincipal();

String className = pjp.getSignature().getDeclaringType().getSimpleName();
String methodName = pjp.getSignature().getName();
String task = className + "." + methodName;

log.info("[Call Method] " + httpMethod.toString() + ": " + task + " | Request userId=" + userId.toString());

Object[] paramArgs = pjp.getArgs();
for (Object object : paramArgs) {
if (Objects.nonNull(object)) {
log.info("[parameter type] {}", object.getClass().getSimpleName());
log.info("[parameter value] {}", object);
}
}

// 해당 클래스 처리 전의 시간
StopWatch sw = new StopWatch();
sw.start();

Object result = null;

// 해당 클래스의 메소드 실행
try{
result = pjp.proceed();
}
catch (Exception e){
log.warn("[ERROR] " + task + " 메서드 예외 발생 : " + e.getMessage());
throw e;
}

// 해당 클래스 처리 후의 시간
sw.stop();
long executionTime = sw.getTotalTimeMillis();

log.info("[ExecutionTime] " + task + " --> " + executionTime + " (ms)");

return result;
}

}
8 changes: 8 additions & 0 deletions main/src/main/resources/console-appender.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<included>
<appender name="CONSOLE"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
</included>
28 changes: 28 additions & 0 deletions main/src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<configuration>
<timestamp key="BY_DATE" datePattern="yyyy-MM-dd"/>
<property name="LOG_PATTERN"
value="[%d{yyyy-MM-dd HH:mm:ss}:%-4relative] %green([%thread]) %highlight(%-5level) %boldWhite([%C.%M:%yellow(%L)]) - %msg%n"/>

<springProfile name="local">
<include resource="console-appender.xml"/>

<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
</springProfile>

<springProfile name="dev">
<include resource="console-appender.xml"/>

<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
</springProfile>

<springProfile name="prod">
<include resource="console-appender.xml"/>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
</springProfile>
</configuration>

0 comments on commit 41f3ea9

Please sign in to comment.