Skip to content

Instantly share code, notes, and snippets.

@zacscoding
Created April 22, 2018 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zacscoding/817c456f392e2b2fc66d24d456a5e985 to your computer and use it in GitHub Desktop.
Save zacscoding/817c456f392e2b2fc66d24d456a5e985 to your computer and use it in GitHub Desktop.
Advice for logging
package org.async.aop;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.async.util.GsonUtil;
import org.async.util.ServletUtil;
import org.springframework.stereotype.Component;
/**
* @author zacconding
* @Date 2018-04-22
* @GitHub : https://github.com/zacscoding
*/
@Slf4j
@Component
@Aspect
public class ControllerLogAdvice {
//@Around("execution(* org.async.web.*Controller.*(..))")
public Object logProcess(ProceedingJoinPoint pjp) throws Throwable {
String id = pjp.getTarget().getClass().getSimpleName() + " :: " + pjp.getSignature().getName();
HttpServletRequest req = ServletUtil.getHttpServletRequest();
log.info("## ======================Before Catch =================================== ##");
log.info("ID : " + id);
log.info("Param : " + GsonUtil.toStringPretty(pjp.getArgs()));
log.info("URI : " + req.getRequestURI());
log.info("IP : " + ServletUtil.getIpAddr(req));
log.info("## ===================================================================== ##");
long elapsed = 0L;
String exception = null;
try {
long start = System.currentTimeMillis();
Object result = pjp.proceed();
long end = System.currentTimeMillis();
elapsed = (end - start);
return result;
} catch (Exception e) {
exception = e.getMessage();
throw e;
} finally {
log.info("## ======================After Catch =================================== ##");
log.info("ID : " + id);
if (exception == null) {
log.info("Elapsed : {} [ms]", elapsed);
} else {
log.info("ERROR : " + exception);
}
log.info("## ===================================================================== ##");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment