Created
April 22, 2018 00:41
-
-
Save zacscoding/817c456f392e2b2fc66d24d456a5e985 to your computer and use it in GitHub Desktop.
Advice for logging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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