Skip to content

Instantly share code, notes, and snippets.

@circlee
Created February 8, 2018 10:39
Show Gist options
  • Save circlee/2eda53d1e154590bbc4f90ff814f345e to your computer and use it in GitHub Desktop.
Save circlee/2eda53d1e154590bbc4f90ff814f345e to your computer and use it in GitHub Desktop.
public class RequestLogAspect {
Logger log = LoggerFactory.getLogger("RequestLogAspect");
@Pointcut("@annotation(com.glowpick.ec.core.annotations.RequestLog)")
public void getRequestLogAnnotation(){
}
@Around("getRequestLogAnnotation()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String methodName = signature
.getMethod()
.getName();
Class<?>[] parameterTypes = signature.getMethod()
.getParameterTypes();
RequestLog annotation = joinPoint.getTarget()
.getClass()
.getMethod(methodName,parameterTypes)
.getAnnotation(RequestLog.class);
if(annotation != null) {
String fileName = annotation.value().getFileName();
// mdc put
MDC.put("fileName", fileName);
}
String threadName = Thread.currentThread().getName() + " : " + System.currentTimeMillis();
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
String method = request.getMethod();
StringBuilder sb = new StringBuilder();
sb.append("\n------------- REQUEST ---");
sb.append(threadName);
sb.append("\n" + method + " url : " + request.getRequestURI());
String requestQueryString = request.getQueryString();
if(requestQueryString != null && requestQueryString.length() != 0) {
sb.append("?");
sb.append(requestQueryString);
}
if(joinPoint.getArgs().length != 0) {
sb.append("\n - method(Arguments) -");
}
Arrays.asList(joinPoint.getArgs()).forEach(arg -> {
sb.append("\n" + (arg != null ? arg.toString() : ""));
});
sb.append("\n-------------------------");
log.debug(sb.toString());
Object result = null;
try {
result = joinPoint.proceed();
} catch (Exception e) {
// exception 용 log 처리
StringBuilder responseSb = new StringBuilder();
responseSb.append("\n------------RESPONSE ---");
responseSb.append(threadName);
responseSb.append("\n Exception : " + e.toString() );
responseSb.append("\n-------------------------");
log.debug(responseSb.toString());
throw e;
}
StringBuilder responseSb = new StringBuilder();
responseSb.append("\n------------RESPONSE ---");
responseSb.append(threadName);
responseSb.append("\n response : " + (result != null ? result.toString() : "") );
responseSb.append("\n-------------------------");
log.debug(responseSb.toString());
// mdc clear
MDC.clear();
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment