Skip to content

Commit

Permalink
chore: Add sessionId and remoteIp
Browse files Browse the repository at this point in the history
  • Loading branch information
namseonu committed Aug 26, 2023
1 parent b4a2035 commit 99c457f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
60 changes: 49 additions & 11 deletions src/main/java/com/momentum/releaser/logging/ReqResAspect.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@
import com.momentum.releaser.global.exception.CustomException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.*;
import org.slf4j.MDC;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.http.ResponseEntity;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.InetAddress;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
Expand All @@ -41,12 +39,12 @@
public class ReqResAspect {
private final ObjectMapper objectMapper;

@Bean
public RequestContextListener requestContextListener() {
return new RequestContextListener();
}
// @Bean
// public RequestContextListener requestContextListener() {
// return new RequestContextListener();
// }

// @Before("execution(* com.momentum.releaser..*.*(..))")
// @Before("execution(* com.momentum.releaser..*.*(..))")
@Pointcut("within(com.momentum.releaser..*)")
void restApiPointCut() {
}
Expand All @@ -63,10 +61,19 @@ public void springBeanPointcut() {
* @return
* @throws Throwable
*/
@Around("restApiPointCut() && springBeanPointcut()")
// @Around("restApiPointCut() && springBeanPointcut()")
@Around("bean(*Controller)")
Object reqResLogging(ProceedingJoinPoint joinPoint) throws Throwable {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

HttpSession session = request.getSession();
String sessionId = session.getId();
String remoteIp = request.getRemoteAddr();

// MDC에 세션 아이디와 IP 주소 설정
MDC.put("sessionId", sessionId);
MDC.put("remoteIp", remoteIp);

// fields
String traceId = (String) request.getAttribute("traceId");
String className = joinPoint.getSignature().getDeclaringTypeName(); // 해당 메서드가 선언된 클래스 이름
Expand Down Expand Up @@ -106,6 +113,7 @@ Object reqResLogging(ProceedingJoinPoint joinPoint) throws Throwable {

// 로그 출력
log.info(objectMapper.writeValueAsString(logging));
log.info("Session ID: {}, Remote IP: {}, Log: {}", sessionId, remoteIp, objectMapper.writeValueAsString(logging));
return result;

} catch (Exception e) {
Expand Down Expand Up @@ -142,4 +150,34 @@ private Map<String, String> getParams(HttpServletRequest request) {

return jsonObject;
}

// @Around("bean(*Controller)")
// public Object controllerAroundLogging(ProceedingJoinPoint pjp) throws Throwable {
// String timeStamp = new SimpleDateFormat(TIMESTAMP_FORMAT).format(new Timestamp(currentTimeMillis()));
// HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
//
// this.clientIp = request.getRemoteAddr();
// this.clientUrl = request.getRequestURL().toString();
//
// String callFunction = pjp.getSignature().getDeclaringTypeName() + "." + pjp.getSignature().getName();
//
// StopWatch stopWatch = new StopWatch();
// stopWatch.start();
//
// Object result = pjp.proceed();
//
// stopWatch.stop();
//
// LogELK logelk = LogELK.builder()
// .timestamp(timeStamp)
// .clientIp(clientIp)
// .clientUrl(clientUrl)
// .callFunction(callFunction)
// .parameter(mapper.writeValueAsString(request.getParameterMap()))
// .responseTime(stopWatch.getTotalTimeSeconds()).build();
//
// log.info("{}", mapper.writeValueAsString(logelk));
// return result;
// }

}
6 changes: 4 additions & 2 deletions src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<!-- 환경 변수 -->
<property name="CONSOLE_LOG_PATTERN"
value="%d{yyyy-MM-dd HH:mm:ss.SSS, ${logback.timezone:-Asia/Seoul}} [%thread] %clr(%5level) %cyan(%logger) - %msg%n"/>
value="%d{yyyy-MM-dd HH:mm:ss.SSS, ${logback.timezone:-Asia/Seoul}} [%thread] %clr(%5level) %cyan(%logger) [%X{sessionId}][%X{remoteIp}] - %msg%n"/>
<property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS, ${logback.timezone:-Asia/Seoul}} [%thread] %5level %logger - %msg%n"/>
<property name="SLACK_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS, ${logback.timezone:-Asia/Seoul}} [%thread] %5level %logger %n %n %msg %n"/>
<springProperty name="SLACK_WEBHOOK_URL_DEV_WARN" source="logging.slack.webhook-url-dev-warn"/>
Expand Down Expand Up @@ -37,7 +37,9 @@
"log.level": "%-5level",
"message": "%message",
"process.thread.name": "%t",
"log.logger": "%C{2}"
"log.logger": "%C{2}",
"sessionId": "%X{sessionId}",
"remoteIP": "%X{remoteIp}"
}
</pattern>
</pattern>
Expand Down

0 comments on commit 99c457f

Please sign in to comment.