com.att.ajsc.common.trace.TrailLoggerAspect Maven / Gradle / Ivy
package com.att.ajsc.common.trace;
import java.lang.reflect.Method;
import java.util.Set;
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.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import com.att.ajsc.common.InterceptorMessages;
import com.att.ajsc.common.Tracable;
import com.att.ajsc.common.TransactionTrail;
import com.att.ajsc.logging.AjscEelfManager;
import com.att.eelf.configuration.EELFLogger;
@Aspect
@Component
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class TrailLoggerAspect {
private static EELFLogger logger = AjscEelfManager.getInstance().getLogger(TrailLoggerAspect.class);
private static final String TRANSACTION_TRAIL = "transactionTrail";
private static final String MILLIS = " millis for ";
private static final String LINE = "line";
@Autowired
private ApplicationContext context;
@Pointcut("@annotation(com.att.ajsc.common.Tracable)")
private void pointcut() {
}
@Around("pointcut()")
public Object logTrail(ProceedingJoinPoint joinPoint) throws Throwable {
long startTimeInMilliseconds = System.currentTimeMillis();
long endTimeInMilliseconds;
long durationInMilliseconds;
String message = null;
String finalMessage = "";
String executionDepth = "-";
String identifier = "";
String placeholder = "";
TransactionTrail transactionTrail;
try {
transactionTrail = (TransactionTrail) context.getBean(TRANSACTION_TRAIL);
} catch (Exception e) {
logger.error(InterceptorMessages.INTERCEPTOR_INVALID_TRAILLOGGER, e, ((MethodSignature) joinPoint.getSignature()).getMethod().getName());
return joinPoint.proceed();
}
try {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
identifier = method.getDeclaringClass().toString() + method.getName();
transactionTrail.addInCompleteMethod(identifier);
long line = transactionTrail.getTrail().split("\n").length + 1;
placeholder = LINE + line + ":";
transactionTrail.setTrail(transactionTrail.getTrail() + "\n" + placeholder);
Tracable tracable = method.getAnnotation(Tracable.class);
message = tracable.message();
if (message.length() == 0) {
message = signature.toString();
}
Object result = joinPoint.proceed();
endTimeInMilliseconds = System.currentTimeMillis();
int inCompleteMethods = incompleteMethods(transactionTrail.getInCompleteMethods());
if (inCompleteMethods > 0) {
for (int i = 0; i < inCompleteMethods; i++) {
executionDepth = executionDepth + "-";
}
}
durationInMilliseconds = endTimeInMilliseconds - startTimeInMilliseconds;
finalMessage = executionDepth + durationInMilliseconds + MILLIS + message;
transactionTrail.setTrail(transactionTrail.getTrail().replace(placeholder, finalMessage));
transactionTrail.getInCompleteMethods().remove(identifier);
return result;
} catch (Throwable e) {
logger.error(InterceptorMessages.INTERCEPTOR_TRAIL_LOGGER_MESSAGE, e, joinPoint.getSignature().toString());
endTimeInMilliseconds = System.currentTimeMillis();
int inCompleteMethods = incompleteMethods(transactionTrail.getInCompleteMethods());
if (inCompleteMethods > 0) {
for (int i = 0; i < inCompleteMethods; i++) {
executionDepth = executionDepth + "-";
}
}
durationInMilliseconds = endTimeInMilliseconds - startTimeInMilliseconds;
finalMessage = executionDepth + durationInMilliseconds + MILLIS + message;
transactionTrail.setTrail(transactionTrail.getTrail().replace(placeholder, finalMessage));
transactionTrail.getInCompleteMethods().remove(identifier);
throw e;
}
}
private int incompleteMethods(Set methods) {
int inCompleteMethods = 0;
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
for (int i = 0; i < elements.length; i++) {
StackTraceElement stackTraceElement = elements[i];
if (methods.contains("class " + stackTraceElement.getClassName() + stackTraceElement.getMethodName())) {
inCompleteMethods++;
}
}
return inCompleteMethods;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy