Last active
October 18, 2015 17:30
-
-
Save davengeo/c0695e90a1510a90aa03 to your computer and use it in GitHub Desktop.
Aspect for profile methods
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
/* | |
* Copyright (c) 2015. | |
* [email protected] | |
*/ | |
package com.daven.aspects; | |
import com.google.common.base.Stopwatch; | |
import org.aspectj.lang.ProceedingJoinPoint; | |
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.stereotype.Component; | |
import java.lang.reflect.Field; | |
import java.util.concurrent.TimeUnit; | |
@Aspect | |
@Component | |
public class LoggingControllerAspect { | |
private static final int MAX_LENGTH_OF_ARGS_FOR_LOGS = 300; | |
private static final String PREFIX = "AOP:*******"; | |
@Around("(anyRepositoryPointcut() && anyComponent())") | |
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { | |
logMethod(pjp); | |
return logElapsedTime(pjp); | |
} | |
private Object logElapsedTime(ProceedingJoinPoint pjp) throws Throwable { | |
Stopwatch stopwatch = Stopwatch.createStarted(); | |
Object returnValue = pjp.proceed(); | |
final Logger logger = getLogger(pjp); | |
logger.info("{}Method:{}:{}", | |
PREFIX, | |
pjp.getSignature().toShortString(), | |
stopwatch.elapsed(TimeUnit.MICROSECONDS)); | |
return returnValue; | |
} | |
private void logMethod(ProceedingJoinPoint pjp) { | |
final Logger logger = getLogger(pjp); | |
logger.info("{}Method:{}", | |
PREFIX, | |
pjp.getSignature().toShortString()); | |
for (Object signatureArg : pjp.getArgs()) { | |
if(signatureArg!=null){ | |
logger.debug("{}Args:{}", | |
PREFIX, | |
truncatedString(signatureArg.toString(), MAX_LENGTH_OF_ARGS_FOR_LOGS)); | |
} | |
} | |
} | |
public static Logger getLogger(org.aspectj.lang.JoinPoint joinPoint) { | |
try { | |
Class declaringType = joinPoint.getSignature().getDeclaringType(); | |
Field loggerField = getLoggerField(declaringType); | |
loggerField.setAccessible(true); | |
return (Logger) loggerField.get(joinPoint.getTarget()); | |
} catch (NoSuchFieldException e) { | |
System.out.println("no logger found"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return LoggerFactory.getLogger(LoggingControllerAspect.class); | |
} | |
//this can be improved, inspecting the class and looking for logger types | |
private static Field getLoggerField(Class declaringType) throws NoSuchFieldException { | |
return declaringType.getDeclaredField("logger"); | |
} | |
private String truncatedString(String string, int numberChar){ | |
return string.length()<numberChar ?string : string.substring(0, numberChar)+"..."; | |
} | |
//change the packaging | |
@Pointcut("within(com.daven.repositories.*)") | |
public void anyRepositoryPointcut() { | |
} | |
@Pointcut("@target(org.springframework.stereotype.Component)") | |
public void anyComponent() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment