Created
November 22, 2018 02:07
-
-
Save qlong8807/3f8cd417c1d8881d89e4606645bc6d02 to your computer and use it in GitHub Desktop.
日志拦截器,打印方法进出详细信息
This file contains hidden or 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
import io.github.jhipster.config.JHipsterConstants; | |
import org.aspectj.lang.JoinPoint; | |
import org.aspectj.lang.ProceedingJoinPoint; | |
import org.aspectj.lang.annotation.AfterThrowing; | |
import org.aspectj.lang.annotation.Around; | |
import org.aspectj.lang.annotation.Aspect; | |
import org.aspectj.lang.annotation.Pointcut; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.core.env.Environment; | |
import java.util.Arrays; | |
/** | |
* Aspect for logging execution of service and repository Spring components. | |
* | |
* By default, it only runs with the "dev" profile. | |
*/ | |
@Aspect | |
public class LoggingAspect { | |
private final Logger log = LoggerFactory.getLogger(this.getClass()); | |
private final Environment env; | |
public LoggingAspect(Environment env) { | |
this.env = env; | |
} | |
/** | |
* Pointcut that matches all repositories, services and Web REST endpoints. | |
*/ | |
@Pointcut("within(@org.springframework.stereotype.Repository *)" + | |
" || within(@org.springframework.stereotype.Service *)" + | |
" || within(@org.springframework.web.bind.annotation.RestController *)") | |
public void springBeanPointcut() { | |
// Method is empty as this is just a Pointcut, the implementations are in the advices. | |
} | |
/** | |
* Pointcut that matches all Spring beans in the application's main packages. | |
*/ | |
@Pointcut("within(com.echinacoop.lightos.repository..*)"+ | |
" || within(com.echinacoop.lightos.service..*)"+ | |
" || within(com.echinacoop.lightos.web.rest..*)") | |
public void applicationPackagePointcut() { | |
// Method is empty as this is just a Pointcut, the implementations are in the advices. | |
} | |
/** | |
* Advice that logs methods throwing exceptions. | |
* | |
* @param joinPoint join point for advice | |
* @param e exception | |
*/ | |
@AfterThrowing(pointcut = "applicationPackagePointcut() && springBeanPointcut()", throwing = "e") | |
public void logAfterThrowing(JoinPoint joinPoint, Throwable e) { | |
if (env.acceptsProfiles(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT)) { | |
log.error("Exception in {}.{}() with cause = \'{}\' and exception = \'{}\'", joinPoint.getSignature().getDeclaringTypeName(), | |
joinPoint.getSignature().getName(), e.getCause() != null? e.getCause() : "NULL", e.getMessage(), e); | |
} else { | |
log.error("Exception in {}.{}() with cause = {}", joinPoint.getSignature().getDeclaringTypeName(), | |
joinPoint.getSignature().getName(), e.getCause() != null? e.getCause() : "NULL"); | |
} | |
} | |
/** | |
* Advice that logs when a method is entered and exited. | |
* | |
* @param joinPoint join point for advice | |
* @return result | |
* @throws Throwable throws IllegalArgumentException | |
*/ | |
@Around("applicationPackagePointcut() && springBeanPointcut()") | |
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable { | |
if (log.isDebugEnabled()) { | |
log.debug("Enter: {}.{}() with argument[s] = {}", joinPoint.getSignature().getDeclaringTypeName(), | |
joinPoint.getSignature().getName(), Arrays.toString(joinPoint.getArgs())); | |
} | |
try { | |
Object result = joinPoint.proceed(); | |
if (log.isDebugEnabled()) { | |
log.debug("Exit: {}.{}() with result = {}", joinPoint.getSignature().getDeclaringTypeName(), | |
joinPoint.getSignature().getName(), result); | |
} | |
return result; | |
} catch (IllegalArgumentException e) { | |
log.error("Illegal argument: {} in {}.{}()", Arrays.toString(joinPoint.getArgs()), | |
joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName()); | |
throw e; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment