Skip to content

Commit

Permalink
Merge pull request #39 from linglong67/feature/log-filter-and-aop
Browse files Browse the repository at this point in the history
[feat] log filter 추가 및 aop 수정
  • Loading branch information
linglong67 authored Jan 10, 2024
2 parents 7233b6e + fc2549f commit 9275fc0
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 41 deletions.
41 changes: 0 additions & 41 deletions module-api/src/main/java/com/kernel360/aop/LogAspect.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.kernel360.global.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LogExecutionTime {
}
24 changes: 24 additions & 0 deletions module-api/src/main/java/com/kernel360/global/aop/LogAspect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.kernel360.global.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class LogAspect {

@Around("@annotation(com.kernel360.global.annotation.LogExecutionTime)")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
long start = System.currentTimeMillis();
Object proceed = joinPoint.proceed();

long executionTime = System.currentTimeMillis() - start;
log.info("##### @LogExecutionTime ##### " + joinPoint.getSignature() + " executed in " + executionTime + "ms");

return proceed;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.kernel360.global.filter;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.ContentCachingResponseWrapper;

import java.io.IOException;

@Slf4j
@Component
public class LogFilter implements Filter {

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
ContentCachingRequestWrapper request = new ContentCachingRequestWrapper((HttpServletRequest) req);
ContentCachingResponseWrapper response = new ContentCachingResponseWrapper((HttpServletResponse) res);
log.info("##### INIT URI: {}", request.getRequestURI());

chain.doFilter(request, response);

// Request
StringBuilder requestHeaderValues = new StringBuilder();
request.getHeaderNames().asIterator().forEachRemaining(headerKey -> {
String headerValue = request.getHeader(headerKey);

requestHeaderValues
.append("[")
.append(headerKey)
.append(" : ")
.append(headerValue)
.append("] ");
});

String requestBody = new String(request.getContentAsByteArray());
String uri = request.getRequestURI();
String method = request.getMethod();
log.info("##### REQUEST ##### uri: {}, method: {}, header: {}, body: {}", uri, method, requestHeaderValues, requestBody);

// Response
StringBuilder responseHeaderValues = new StringBuilder();
response.getHeaderNames().forEach(headerKey -> {
String headerValue = response.getHeader(headerKey);

responseHeaderValues
.append("[")
.append(headerKey)
.append(" : ")
.append(headerValue)
.append("] ");
});

String responseBody = new String(response.getContentAsByteArray());
log.info("##### RESPONSE ##### uri: {}, method: {}, header: {}, body: {}", uri, method, responseHeaderValues, responseBody);

response.copyBodyToResponse();
}
}

0 comments on commit 9275fc0

Please sign in to comment.